ENTERPRISE

Microsoft Dynamics AX 2009 : Building Lookups - Creating a lookup dynamically

9/15/2012 2:54:39 AM
Standard automatic lookups are widely used across the system, but sometimes it is required to show different fields from different data sources, apply various static or dynamics ranges, and so on. It is not possible to achieve such results by modifying or creating table relations, changing TitleField1 or TitleField2 properties, or editing the AutoLookup group. But Dynamics AX allows creating custom lookups either using AOT forms or dynamically generating them from X++ code. This recipe will demonstrate the latter option, which uses the SysTableLookup application class to build a runtime lookup from X++ code.

In standard Dynamics AX, every customer account can be assigned to a vendor account and vice versa. In this example, we will modify the Vendor account lookup on the Customer Details form to allow users to select only vendors that use the same currency as the customer.

How to do it...

  1. 1. Open VendTable table in AOT, and create a new method:

    public static void lookupVendorByCurrency(
    FormControl _callingControl,
    CurrencyCode _currency)
    
    {
    Query query;
    QueryBuildDataSource qbds;
    QueryBuildRange qbr;
    SysTableLookup lookup;
    ;
    query = new Query();
    qbds = query.addDataSource(tablenum(VendTable));
    qbr = qbds.addRange(fieldnum(VendTable, Currency));
    qbr.value(queryvalue(_currency));
    lookup = SysTableLookup::newParameters(
    tablenum(VendTable),
    _callingControl,
    true);
    lookup.parmQuery(query);
    lookup.addLookupField(
    fieldnum(VendTable, AccountNum),
    true);
    Vendor account lookupVendor account lookupmodifyinglookup.addLookupField(fieldnum(VendTable, Name));
    lookup.addLookupField(fieldnum(VendTable, Currency));
    lookup.performFormLookup();
    }
    
    
    					  
  2. 2. Open the CustTable form in AOT, and override the lookup() method of the VendAccount field on the CustTable data source with the following code:

    public void lookup(FormControl _formControl, str _filterStr)
    
    {;
    VendTable::lookupVendorByCurrency(
    _formControl,
    CustTable.Currency);
    }
    
  3. 3. In AOT, the form should look as shown below:

  1. 4. To test this, open Accounts receivable | Customer Details, and expand the Vendor account lookup located on the General tab page. The lookup is now different— it has the additional column Currency and vendors in the list match the customer currency:

How it works...

First, we create a method that generates the lookup on the VendTable table. That is a most convenient place for such a method, taking into consideration that it may be reused at a number of places.

In this method, we first create a new query, which will be the base for lookup records. We add a new VendTable data source to it, define a new Currency range, and set its value to the _currency argument.

Next, we create the actual lookup object and pass the query object through the parmQuery() member method. The lookup object is created by using the newParameters() constructor of SysTableLookup. This method accepts three parameters:

  1. 1. The Table ID, which is going to be displayed.

  2. 2. A reference to the form calling the control.

  3. 3. An optional Boolean value, which specifies that the current control value should be highlighted in the lookup. The default is true.

Lookup columns are defined using the addLookupField() member method. We add three columns— Vendor account, Name, and Currency. This method accepts the following parameters:

  1. 1. The field ID of the field that will be displayed as a column.

  2. 2. An optional Boolean value that defines which column value is returned to the caller form upon user selection. The default is false.

And finally, we run the lookup by calling the performFormLookup() member method.

Now, we need to add some code to the lookup() of the VendAccount field of the CustTable data source in the CustTable form. Before the modification, Vendor account lookup was generated automatically by the system. We are changing this behavior by overriding the super() and calling the previously created method.
Other  
  •  Microsoft Dynamics AX 2009 : Building Lookups - Creating an automatic lookup
  •  Introducing Our New Zero-Point
  •  Exchange Server 2010 Administration Overview (part 3) - Using the Graphical Administration Tools, Using the Command-Line Administration Tools
  •  Exchange Server 2010 Administration Overview (part 2) - Exchange Server and Windows,Exchange Server and Active Directory
  •  Exchange Server 2010 Administration Overview (part 1) - Exchange Server 2010 and Your Hardware, Exchange Server 2010 Editions
  •  Touch Screens Are Everywhere
  •  Intel Builds Bridge to Next - Generation Macs
  •  Microsoft vs. Google vs. Apple: Who will win?
  •  Phone Business: The Age Of Convergence
  •  Microsoft Visual Basic 2008 : Services That Listen - Allowing Multiple Connections
  •  Microsoft Visual Basic 2008 : Services That Listen - Listening with TCP/IP
  •  Seagate GoFlex Satellite Wireless
  •  Toshiba MQ01ABD100 1TB Hard Drive
  •  View Quest Retro WI-FI Radio
  •  Microsoft Enterprise Library : Non-Formatted Trace Listeners (part 3) - Adding Additional Context Information, Tracing and Correlating Activities
  •  Microsoft Enterprise Library : Non-Formatted Trace Listeners (part 2) - Logging to a Database, Testing Logging Filter Status
  •  Microsoft Enterprise Library : Non-Formatted Trace Listeners (part 1) - Creating and Using Logentry Objects, Capturing Unprocessed Events and Logging Errors
  •  Introducing Windows Presentation Foundation and XAML : Transforming Markup into a .NET Assembly
  •  Introducing Windows Presentation Foundation and XAML : Building a WPF Application using Only XAML
  •  Intel : We For One Welcome Our Apple Overlords
  •  
    Most View
    The Perfect Enclosure (Part 3) : Corsair Graphite 600T, Corsair Vengeance C70
    Windows 8 Hardware (Part 2) : Lenovo Ideapad Yoga 13, Dell XPS 12
    Sigma 120-300mm F/2.8 DG OS HSM S Lens Review
    Fujifilm X-E1 - A Retro Camera That Inspires (Part 5)
    Learn How To... Protect And Copy Your Optical Discs
    The Truth About Free Trials (Part 2)
    Musical Fidelity M1 CLiC Universal Music Controller (Part 2)
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 1)
    What If You Don’t Like Pi? (Part 2)
    Nokia Lumia 920 - Windows Phone 8 And Magic Camera (Part 1)
    Top 10
    Windows Management and Maintenance : The Windows 7 Control Panel (part 11) - Region and Language, System
    Windows Management and Maintenance : The Windows 7 Control Panel (part 10) - Programs and Features
    Windows Management and Maintenance : The Windows 7 Control Panel (part 9) - Notification Area Icons, Performance Information and Tools
    Windows Management and Maintenance : The Windows 7 Control Panel (part 8) - Fonts
    Windows Management and Maintenance : The Windows 7 Control Panel (part 7) - Ease of Access Center
    Windows Management and Maintenance : The Windows 7 Control Panel (part 6) - Devices and Printers
    Windows Management and Maintenance : The Windows 7 Control Panel (part 5) - AutoPlay
    Windows Management and Maintenance : The Windows 7 Control Panel (part 4) - AutoPlay
    Windows Management and Maintenance : The Windows 7 Control Panel (part 3) - Action Center
    Windows Management and Maintenance : The Windows 7 Control Panel (part 2)