Monday, 9 March 2015

Using Multi-Select Lookup Control in Microsoft Dynamics AX 2012

Overview

Lookups are used to select values for a control in Microsoft Dynamics AX. The values can be records from a reference table or from multiple data sources in Microsoft Dynamics AX.
The lookup form is opened whenever the lookup button is pressed by a user on any field in Microsoft Dynamics AX. There are multiple methods to create a lookup form. This tutorial will discuss a method to create a lookup form in which multiple values can be selected for a single control.

Pre-requisites

  • Microsoft Dynamics AX 2012

Scenario

As part of this tutorial, the user will be able to select multiple customers from a single lookup

Steps

  1. First of all create a new Form. Open AOT àForms
  2. Right click on Forms, click New Form and name it TestMultiLookup
  3. Navigate to the Design node, right click it, then select a StringEdit control under New Control
  4. Name the control MultiLookup and set its properties to those shown below
  5. Similarly, add a new Button and name it GetValues and set its Text property as Values
  6. The final design should resemble the one shown below
  7. Now open the Class declaration node of the form and write the following code into it
    SysLookupMultiSelectCtrl msCtrlCust;
  8. Override the init method of the form and write the following code in it
  9. Query query = new Query();
    QueryBuildDataSource qbds;
    super();
    //creating query to show customer account and customer name
    //the query must contain only those fields that should be visible on the lookup
    qbds = query.addDataSource(tableNum(CustTable));
    qbds.fields().dynamic(YesNo::No);
    qbds.fields().addField(fieldNum(CustTable,AccountNum));
    qbds = qbds.addDataSource(tableNum(DirPartyTable));
    qbds.fields().dynamic(YesNo::No);
    qbds.fields().addField(fieldNum(DirPartyTable,Name));
    qbds.relations(true);
    //assigning control and query to the class
    msCtrlCust = SysLookupMultiSelectCtrl::constructWithQuery(element, MultiLookup, query);
  10. Override the clicked method of the button GetValues and write the following code in it
  11. container values = msCtrlCust.get(); // get the rec Ids of the Cust table. To get the display value, use the getSelectedFieldValues method
    int i;
    super();
    //loop through the container and show the selected Ids in the info log
    for (i = 1; i <= conLen(values);i++)
    {
    info(conPeek(values,i));
    }
  12. The multi-select lookup control is implemented. To test it, run the form
  13. Open the lookup on the Customers field. Multiple records can be selected by checking the check box on the grid
  14. Press the Ok button to select the checked records
  15. The selected values will be shown on the Customers field.
  16. To see the record Ids (RecId) for the Customers (CustTable), press the Values button
  17. The Ids will be shown in the Infolog
Note: To remove the selected record(s), they need to be unchecked on the lookup. The Customers field will become non-editable when associated with the multi-select lookup control and hence, no change can be made on the field

No comments:

Post a Comment