How to Override the Lookup Method in D365 X++ Form String Control
How to Override the Lookup Method in D365 X++ Form String Control
Introduction
In this article, we will examine scenarios where overriding the lookup method becomes essential and walk through the steps to implement this in D365.
When Should You Override the Lookup Method?
Although automatic lookup forms are convenient, there are specific instances when customization is necessary:
- No Relation Between Tables: Some tables may not have a logical relationship, making the default setup unsuitable.
- String Controls Not Linked to Data Sources: When using string controls for filtering above grids, standard lookups might not suffice.
- Dynamic Column Display: Users may need different columns in lookup forms based on the primary form. While the AutoLookup property allows some flexibility, developers can enhance this further by overriding the lookup method.
- Record Filtering: There may be scenarios where developers want to apply specific filters beyond the default setup provided by the relations node.
Understanding how to override the lookup method is crucial in these situations.
Example Scenario: Filtering Model ID Lookup
Let's consider a practical example with a form called "Vehicle Service Workbench." On this form, users input vehicle records, including Vehicle ID, Make ID, and Model ID. After entering the Make ID, we want to filter the Model ID lookup to display only models associated with the selected Make ID.
Currently, clicking the Model ID dropdown reveals all available models. We will now implement a filter to refine this selection.
Which Lookup Method to Override?
To customize the lookup, we need to override the lookup method for the Model ID field. While it is possible to override the method at the form control level, doing so only affects that specific control. For a consistent approach across the form, we will override the method on the Data Source field.
- Open the form designer in D365.
- Expand the Data Sources node in the left pane.
- Locate the Data Source containing the Model ID field (for instance,
rsmVehicle). - Expand the Model ID node, select the Methods node, right-click, and choose Override > Lookup.
The system will generate a lookup method, which we will modify to suit our needs.
Filtering Records Shown in the Lookup Form
To filter the records in the lookup, replace the auto-generated lookup method with the following code:
public void lookup(FormControl _formControl, str _filterStr)
{
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(rsmModel), _formControl);
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
queryBuildDataSource = query.addDataSource(tableNum(rsmModel));
queryBuildRange = queryBuildDataSource.addRange(fieldNum(rsmModel, MakeId));
queryBuildRange.value(queryValue(rsmVehicle.MakeId));
sysTableLookup.addLookupField(fieldNum(rsmModel, ModelId));
sysTableLookup.addLookupField(fieldNum(rsmModel, Description));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
This code creates a query that filters the rsmModel table based on the Make ID entered in the rsmVehicle form.
Controlling Which Columns Show in the Lookup Form
Sometimes, you may only wish to modify the columns displayed in the lookup. For instance, if we decide not to show the IsServiceable field, we can remove the corresponding line from the code:
public void lookup(FormControl _formControl, str _filterStr)
{
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(rsmModel), _formControl);
sysTableLookup.addLookupField(fieldNum(rsmModel, ModelId));
sysTableLookup.addLookupField(fieldNum(rsmModel, Description));
sysTableLookup.performFormLookup();
}
After recompiling, only the specified columns will be shown in the lookup form.
Handling Cases Where Make ID Is Not Specified
A potential issue arises when a user has not entered a Make ID; in this case, the lookup displays no records. To address this, modify the filtering logic as follows:
if (rsmVehicle.MakeId != "")
{
queryBuildRange.value(queryValue(rsmVehicle.MakeId));
}
This adjustment ensures that the lookup functions correctly, displaying all records when the Make ID is not set.
Conclusion
In this article, we explored how to override the lookup method in D365. We identified scenarios where customization is necessary and demonstrated how to filter records and control which columns appear in the lookup form. Understanding these techniques allows developers to create more intuitive and functional applications within D365.
In future articles, we will look at further extending the lookup functionality. I hope this guide has provided valuable insights into customizing your D365 experience!
Comments
Post a Comment