How to create multi select lookup in the form filter control.
Scenario: Imagine a scenario where we need to select multiple values from a form control lookup in Dynamics 365 Finance and Operations (D365 FO).
High level resolution steps
- Create the form and the filter control.
- Implement the logic on the filter control to select multiple values.
- Use the multiple values to filtering or other purpose based on the requirement.
Detailed resolution steps
Step1: Create the form, custom filter group and add the filter control.
Step 2: Once the filter control is created, override the control lookup method to add the multi select logic.
[Control("String")]
class CustAccount
{
public void lookup()
{
super();
Query query = new Query();
QueryBuildDataSource custTableDS;
//To provide the data source
custTableDS = query.addDataSource(tableNum(CustAccountName));
// adding fields to groupby to select the unquie values
custTableDS.addSortField(fieldNum(CustAccountName, AccountNum));
custTableDS.addSortField(fieldNum(CustAccountName, Name));
custTableDS.orderMode(OrderMode::GroupBy);
//select the fields to display in the lookup
custTableDS.addSelectionField(fieldNum(CustAccountName, AccountNum));
custTableDS.addSelectionField(fieldNum(CustAccountName, Name));
//providing the constructed query to the multi select lookup
custAccountMultiCtrl = SysLookupMultiSelectCtrl::constructWithQuery(this.formRun(), this, query);
//The values which are selected in the control setting those values to the multi select control field
custAccountMultiCtrl.set(custAccountMultiCtrl.getSelectedFieldValues());
}
}
Step 3: Once the multi select lookup values are set successfully to the control. Retrieve those values for filtering or other purpose based on your requirement.
[DataSource]
class CustTable
{
public void executeQuery()
{
Query query = new Query();
QueryBuildDataSource custTableDS;
// adding the datasource to the query object.
custTableDS = query.addDataSource(tableNum(CustTable));
//checking the condition that both mullti select control and the CustAccount control contains the values
if(custAccountMultiCtrl && CustAccount.valueStr())
{
// getting all the selected values from the mullti select control
container custAccountcontainer = custAccountMultiCtrl. getSelectedFieldValues();
int i;
//looping through all the values and filtering on the data source.
while(i <= conLen(custAccountcontainer))
{
custTableDS.addRange(fieldNum(CustTable, AccountNum)).value(queryValue(conPeek(custAccountcontainer, i)));
}
}
//providing constructed query to the CustTable data source
this.query(query);
super();
}
}
Output
Navigate to the form in the UI.
Select the multiple values in the lookup are used to filter in the grid.
The multiple values selected in the lookup control are used to filter the data source and display the results in the grid.