programming4us
programming4us
ENTERPRISE

Microsoft Dynamics AX 2009 : Building Lookups - Selecting a file

12/2/2012 6:31:13 PM
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. 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. 2. Then add it to the PurchaseOrder field group.

  3. 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. 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. 1. fileNameLookupTitle() contains text to be displayed as a lookup title.

  2. 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. 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. 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. 1. A handler to the calling window.

  2. 2. A container of allowed file extensions. It is exactly what fileNameLookupFilter() returns.

  3. 3. The lookup title.

  4. 4. The default file extension.

  5. 5. The preselected file path.

After all those modifications, the appearance of the lookup stays the same.

Other  
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 3) - Sponsorship Management
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 2) - Providing a Sponsor, Leasing and Remote Activation Modes
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 1) - Lease Properties, Configuring a Lease, Renewing a Lease
  •  DisplayPort 1.2 - Meet HDMI’s Smarter Brother
  •  HP ProLiant Servers AIS : Processors and Multiprocessing - The Processor Performance Evolution
  •  HP ProLiant Servers AIS : Processors and Multiprocessing - How Processors Work
  •  How To Get The Best From Your Batteries (Part 2)
  •  How To Get The Best From Your Batteries (Part 1)
  •  The 30 Most Important Technology Trends (Part 3)
  •  The 30 Most Important Technology Trends (Part 2)
  •  
    video
     
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    programming4us
     
     
    programming4us