The post Using Multi-Select Lookup Control in Microsoft Dynamics AX 2012 appeared first on Dynamics 101.

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

  • Navigate to the Design node, right click it, then select a StringEdit control under New Control

  • Name the control MultiLookup and set its properties to those shown below

  • Similarly, add a new Button and name it GetValues and set its Text property as Values
  • The final design should resemble the one shown below

  • Now open the Class declaration node of the form and write the following code into it

    SysLookupMultiSelectCtrl msCtrlCust;

  • Override the init method of the form and write the following code in it

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);

  • Override the clicked method of the button GetValues and write the following code in it

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));

}

  • The multi-select lookup control is implemented. To test it, run the form
  • Open the lookup on the Customers field. Multiple records can be selected by checking the check box on the grid
  • Press the Ok button to select the checked records

  • The selected values will be shown on the Customers field.
  • To see the record Ids (RecId) for the Customers (CustTable), press the Values button

  • 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