This recipe will show several
approaches to create a file lookup using standard Dynamics AX.
As an example, we will create a Terms & conditions control in the Form setup form in Account payable, which allows storing a path to the text document.
How to do it...
1. Open VendFormLetterParameters table in AOT, and create a new field with properties:
Property
|
Value
|
---|
Type
|
String
|
Name
|
TermsAndConditions
|
ExtendedDataType
|
FilenameOpen
|
Label
|
Terms & conditions
|
2. Then add it to the PurchaseOrder field group. 3. Next, open the VendFormletterParameters form in AOT, and create the following methods: str fileNameLookupTitle()
{
return "Select Terms & conditions document";
}
str fileNameLookupInitialPath()
{
container file;
file lookupfile lookupcreating;
file = fileNameSplit(
VendFormletterParameters.TermsAndConditions);
return conpeek(file ,1);
}
str fileNameLookupFilename()
{
Filename path;
Filename name;
Filename type;
;
[path, name, type] = fileNameSplit(
VendFormletterParameters.TermsAndConditions);
return name + type;
}
container fileNameLookupFilter()
{
#File
;
return [WinAPI::fileType(#txt),#AllFilesName+#txt];
}
4. As a result, we should be able to select and store a text file in the Account payable | Setup | Forms | Form setup form under the Purchase order tab page:
How it works...
The extended data type FilenameOpen
of the newly created table field is bound to a standard Dynamics AX
file lookup form, which presents the user with the file selection
dialog.
By adding this field to the PurchaseOrder field group in the VendFormLetterParameters table, we ensure that it is displayed on the Form setup form in the Layout group under the Purchase order tab.
The following four form methods are called by the lookup and must be present on the caller form:
1. fileNameLookupTitle() contains text to be displayed as a lookup title.
2. fileNameLookupInitialPath() is used to identify the initial directory. If there is a value in the Terms & conditions
field, this method strips the filename part and returns a directory
path to the lookup to be opened as the starting browsing point. Here, we
use the global function filenamesplit() to process the stored file path. If the field is empty, the lookup starting point is the last used folder.
3. fileNameLookupFilename() detects the current value in the field and extracts the filename to be displayed on the lookup. We use the global fileNameSplit()
function again to separate given directory path into three
parts— directory path, filename, and file extension. For
example, if the current Terms & conditions value is \\LONDON\Documents\terms.txt, once the user clicks on the lookup button, the method returns only the filename terms.txt (file name + file extension) separated from the rest of the directory path.
4. fileNameLookupFilter()
is responsible for displaying a list of allowed file extensions. It
returns a container of allowed extensions in pairs of two. The first,
third, fifth, etc value is the name of the file extension and the
second, fourth, sixth, etc is an extension filter. In this example, only
text files are allowed and the method returns two values in the
container. The first value is a string Text Document and the second one is *.txt. In order to avoid literals in X++ code, we use the #File macro definitions #txt and #AllFileName, which respectively contain the string .txt and *, which are concatenated by the lookup to present the user with the Text Document (*.txt) filter. WinAPI::fileType() according to current Windows registry settings converts file extensions to textual representation.
There's more...
The previous technique
requires creating a number of methods on the caller form and will not
work with multiple file lookups on the same form. A different approach
could be used to avoid those issues. Let's modify the previous example
by removing all four methods from the form itself and overriding the lookup() method on the TermAndConditions field of the VendFormLetterParameters data source:
public void lookup(FormControl _formControl, str _filterStr)
{
FilenameOpen file;
Filename path;
Filename name;
Filename type
#File
;
[path, name, type] = filenamesplit(
VendFormLetterParameters.TermsAndConditions);
file = WinAPI::getOpenFileName(
element.hWnd(),
[WinAPI::fileType(#txt),#AllFilesName+#txt],
path,
"Select Terms & conditions document",
"",
name + type
);
if (file)
{
VendFormLetterParameters.TermsAndConditions = file;
VendFormLetterParameters_ds.refresh();
}
}
This method calls getOpenFileName() method of WinAPI application class, which opens the standard Windows file selection dialog. The method accepts a number of arguments:
1. A handler to the calling window. 2. A container of allowed file extensions. It is exactly what fileNameLookupFilter() returns. 4. The default file extension. 5. The preselected file path.
After all those modifications, the appearance of the lookup stays the same.
|