Override the lookup method on Formdatasource field(on which you want to show lookup) , and copy the following code to your method.
Comment the super() method in the lookup.
public void lookup(FormControl _formControl, str _filterStr)
{
SysTableLookup sysTableLookup; // systemclass to create //customlookup
Query query;
QueryBuildDataSource qbd;
;
sysTableLookup = SysTableLookup::newParameters(tablenum(InventTable),_formcontrol);
// Construct query on the table,
// whose records you want to show as lookup.
query = new Query();
qbd = query.addDataSource(tablenum(InventTable));
qbd.addRange(fieldnum(InventTable,ItemType)).value(SysQuery::value(enum2str
(ItemType::Item)));
// add the fields to the lookup list
sysTableLookup.addLookupfield(fieldnum(InventTable,ItemId));
sysTableLookup.addLookupfield(fieldnum(InventTable,ItemName));
// pass the query as parameter
// system will show the records in the lookup
// as per your query
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
public void lookup(FormControl _formControl, str _filterStr)
{
SysTableLookup sysTableLookup; // systemclass to create //customlookup
Query query;
QueryBuildDataSource qbd;
;
sysTableLookup = SysTableLookup::newParameters(tablenum(InventTable),_formcontrol);
// Construct query on the table,
// whose records you want to show as lookup.
query = new Query();
qbd = query.addDataSource(tablenum(InventTable));
qbd.addRange(fieldnum(InventTable,ItemType)).value(SysQuery::value(enum2str
(ItemType::Item)));
// add the fields to the lookup list
sysTableLookup.addLookupfield(fieldnum(InventTable,ItemId));
sysTableLookup.addLookupfield(fieldnum(InventTable,ItemName));
// pass the query as parameter
// system will show the records in the lookup
// as per your query
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
Building Lookups - Using a form for lookup building
In numerous situations, standard, automatic, or even dynamic runtime lookups cannot display the required data. For example, it might be a lookup with tab pages or a search field. In such cases, Dynamics AX offers the possibility to create an AOT form and use it as lookup.
In this recipe, we will demonstrate how to create a lookup using an AOT form. As an example, we will modify the standard customer account lookup to display only active customers.
How to do it...
-
-
2. Change the form's following design properties:
Property Value
Frame Border
WindowType Popup
-
-
4. Add a new StringEdit control to the grid:
Property Value
Name AccountNum
DataSource CustTable
DataField AccountNum
AutoDeclaration Yes
-
5. Add another StringEdit control to the grid, right after AccountNum:
Property Value
Name Name
DataSource CustTable
DataField Name
-
6. Add one more StringEdit control to the grid, right after Name:
Property Value
Name Phone
DataSource CustTable
DataField Phone
-
-
-
9. Override the form's run() with the following code:
public void run()
{
FormStringControl callingControl;
boolean filterLookup;
;
callingControl = SysTableLookup::getCallerStringControl(
element.args());
filterLookup = SysTableLookup::filterLookupPreRun(
callingControl,
AccountNum,
CustTable_ds);
super();
SysTableLookup::filterLookupPostRun(
filterLookup,
callingControl.text(),
AccountNum,
CustTable_ds);
}
-
10. Finally, override init() of the CustTable data source with the following code:
public void init()
{
Query query;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
;
query = new Query();
qbds = query.addDataSource(tablenum(CustTable));
qbr = qbds.addRange(fieldnum(CustTable, Blocked));
qbr.value(queryvalue(CustVendorBlocked::No));
this.query(query);
}
-
- 2. Change the form's following design properties:
Property Value Frame Border WindowType Popup
- 4. Add a new StringEdit control to the grid:
Property Value Name AccountNum DataSource CustTable DataField AccountNum AutoDeclaration Yes
- 5. Add another StringEdit control to the grid, right after AccountNum:
Property Value Name Name DataSource CustTable DataField Name
- 6. Add one more StringEdit control to the grid, right after Name:
Property Value Name Phone DataSource CustTable DataField Phone
- 9. Override the form's run() with the following code:
public void run() { FormStringControl callingControl; boolean filterLookup; ; callingControl = SysTableLookup::getCallerStringControl( element.args()); filterLookup = SysTableLookup::filterLookupPreRun( callingControl, AccountNum, CustTable_ds); super(); SysTableLookup::filterLookupPostRun( filterLookup, callingControl.text(), AccountNum, CustTable_ds); } - 10. Finally, override init() of the CustTable data source with the following code:
public void init() { Query query; QueryBuildDataSource qbds; QueryBuildRange qbr; ; query = new Query(); qbds = query.addDataSource(tablenum(CustTable)); qbr = qbds.addRange(fieldnum(CustTable, Blocked)); qbr.value(queryvalue(CustVendorBlocked::No)); this.query(query); }
-
12. Locate the extended data type CustAccount in AOT, and change its property:
Property Value
FormHelp CustLookup
-
13. To test the results, open Accounts receivable | Sales Order Details and start creating a new sales order. Notice that now Customer account lookup is different, and it includes only active customers:
- 12. Locate the extended data type CustAccount in AOT, and change its property:
Property Value FormHelp CustLookup
- 13. To test the results, open Accounts receivable | Sales Order Details and start creating a new sales order. Notice that now Customer account lookup is different, and it includes only active customers:

