How to: Build Dynamic Lookup

Posted on Updated on

Dynamics AX is flexible enough that the developer can create custom lookups by running them dynamically from the X++ code.Today I will show how to dynamically create a lookup through X++.
We will modify the Vendor account lookup on the Customers form to allow users to select only those vendors that use the same currency as the currently selected customer.

Keep in mind that we can create a lookup directly on form but it’s a good practice to do all we can on the Table.

1. Open the VendTable table in the 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);
    lookup.addLookupField(fieldNum(VendTable,Party));
    lookup.addLookupField(fieldNum(VendTable,Currency));
    lookup.performFormLookup();
}

2. In the AOT, open the CustTable form, 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. To test this, open Accounts receivable > Customers > All customers, select any of the customers, and click on the Edit button in the action pane. Once the Customers form is displayed, expand the Vendor account lookup located in the Miscellaneous details tab page, under the Remittance group. The modified lookup now has an additional column named Currency, and vendors in the list should match the customer’s currency:

Lookup - Results

Leave a comment