AX / D365FO – How to: Add a Lookup Form to a Control in X++

You can use X++ to add a custom lookup form to a control. To achieve this, you override the lookup method of the control. This allows you to create a query and define a lookup form specific to the control. However, note that the lookup functionality will only be available for the specified control and cannot be shared with other controls or forms.

Note
For a ReferenceGroup control, use the lookupReference method instead of the lookup method to add a lookup form.

The following steps demonstrate how to override the lookup method for a StringEdit control and utilize the SysTableLookup class to implement a custom lookup form.


Adding a Lookup Form to a StringEdit Control

  1. Add a StringEdit Control
    • In the AOT, navigate to Forms, expand the form, expand Designs, right-click Design, and select New Control > StringEdit.
    • A StringEdit control will be added to the form.
  2. Set Control Properties
    • Right-click the control and select Properties. Review and update the following properties:
PropertyDescription
DataFieldBind the control to a field in the form’s data source by selecting the field name.
DataSourceSpecify the table containing the field you want to bind.
LabelSelect a label describing the information displayed in the control.
NameProvide a unique name for the control.

3. Override the lookup Method

  • Expand the control, right-click Methods, select Override Method, and choose lookup.
  • The lookup method opens in the code editor.

4. Add Query Objects

  • Add Query, QueryBuildDataSource, and QueryBuildRange objects to define the query for the lookup form. Example:
public void lookup()
{
    Query query = new Query();
    QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange;
}

5. Create a SysTableLookup Instance

  • Use the SysTableLookup class to create the lookup form. Example:
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(CustTable), this);

6. Specify Fields for the Lookup

  • Use the addLookupField method to define the fields displayed in the lookup form. Example:
sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup));

7. Define Query for Lookup Data

  • Add a range to the query to filter the data. Example:
queryBuildDataSource = query.addDataSource(tableNum(CustTable));
queryBuildRange = queryBuildDataSource.addRange(fieldNum(CustTable, CustGroup));
queryBuildRange.value('40');
sysTableLookup.parmQuery(query);

8. Display the Lookup Form

  • Use the performFormLookup method to open the lookup form. Example:
sysTableLookup.performFormLookup();

9. Comment out the super Call

  • To prevent the standard lookup from displaying, comment out the super call. Example:
//super();

10. Save and Test the Form

  • Right-click the form and select Save.
  • Open the form, locate the StringEdit control, and click the arrow to see the custom lookup form.

Complete Code Example

The following example demonstrates a complete implementation of a custom lookup for customers:

public void lookup()
{
    Query query = new Query();
    QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange; 
 
    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(CustTable), this); 
 
    sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
    sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup)); 
 
    queryBuildDataSource = query.addDataSource(tableNum(CustTable));
 
    queryBuildRange = queryBuildDataSource.addRange(fieldNum(CustTable, CustGroup));
    queryBuildRange.value('40');
 
    sysTableLookup.parmQuery(query);
 
    sysTableLookup.performFormLookup();
 
    //super();
}

Now, your StringEdit control will display a custom lookup form filtered to show customers in group 40.

Leave a comment