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. 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. 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. In AOT, the form should look as shown below:
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. The Table ID, which is going to be displayed.
2. A reference to the form calling the control.
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. The field ID of the field that will be displayed as a column.
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.