Share via


How to: Add a Lookup Form to a Control

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

You can use X++ to add a lookup form to a control. To create a custom lookup form, you override the lookup method of the control. You can then create a query and lookup form you can use to populate the control. However, the lookup that appears is associated only with the specified control and is not available to any other control or form.

Note

To add a lookup form to a ReferenceGroup control, use the lookupReference method for the control. The lookup method of the ReferenceGroup control is not used.

The following section shows how to override the lookup method of a StringEdit control and how to use the SysTableLookup class to create a lookup form.

To Add a Lookup Form to a StringEdit Control

  1. In the AOT, expand Forms, expand the form, expand Designs, right-click Design, click New Control, and then click StringEdit. A StringEdit control is added to the form.

  2. Right-click the control, and then click Properties. Review the following properties.

    Property

    Description

    DataField

    If you want to bind the control to a field in the form data source, select the name of the field.

    Note

    To see a list of fields, set the DataSource property before you view the DataField list.

    DataSource

    If you want to bind the control to a field in the form data source, select the name of the table that contains the field.

    Label

    Select a label that describes the information that appears in the control.

    Name

    Specify a name that uniquely identifies the control.

  3. Expand the control, right-click Methods, click Override method, and then click lookup. The lookup method opens in the code editor.

  4. In the code editor, add Query, QueryBuildDataSource, and QueryBuildRange objects. The following code example adds the classes you use to construct the query for the lookup form.

        public void lookup()
        {
            Query query = new Query();
            QueryBuildDataSource queryBuildDataSource;
            QueryBuildRange queryBuildRange; 
  1. Create an instance of SysTableLookup class. The following code example creates a lookup form for customers. Notice how this in the example represents the current form control.
        SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(custTable), this); 
  1. Use the addLookupField method to specify the fields that appear in the lookup form. The following code example adds the AccountNum and CustGroup fields to the lookup form.
        sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
        sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup)); 
  1. Use a query to retrieve data for the lookup form. The following code example uses a range limit so that the lookup form lists customers who have the customer group of 40.
        queryBuildDataSource = query.addDataSource(tableNum(CustTable));
    
        queryBuildRange = queryBuildDataSource.addRange(fieldNum(CustTable, CustGroup));
        queryBuildRange.value('40');
    
        sysTableLookup.parmQuery(query);
  1. Use the performFormLookup method to open the lookup form.
        sysTableLookup.performFormLookup();
  1. When you override the lookup method, comment out the call to super. If you do not comment out the call to super, the standard lookup form might appear.
        //super();
    }
  1. Right-click the form and then click Save. To see the lookup form, right-click the form and then click Open. Find the StringEdit control and then click the arrow to open the lookup form.

The following code example shows the complete lookup method 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();
    }

See also

Lookup Forms Overview

How to: Add a Control with a Lookup Form

How to: Add a Run-Time Lookup Form

Methods on Form Controls

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.