A lookup method in D365 forms allows users to view multiple columns from a related record to help with their selection. In this article I will show you how to override the lookup method in D365 to show different columns, and a different number of records.
Automatic Lookup
In this previous article I explained when to lookups are used. I also explained how to let the system automatically create the lookup forms for you. This is extremely helpful and makes creating lookups very easy. So be sure to read that article first.
In this article I will show you how to override the lookup method in D365. Let’s look at some of the scenarios of when you might need to override the lookup method in D365.
When Do I need To Override The Lookup Method?
Although it is really great that the lookup form is generated for us, there are occasions when you may want to change what is shown for a particular form. Here are some examples.
- Occasionally there are scenarios where it doesn’t make sense to setup a relation between two tables.
- Other times we have a string control that is not tied to a Data Source. Such as when we want to use a string control as filter above a grid.
- Also, a user may want to show a different number of columns in the lookup form, depending on what main form they are on. They can specify the columns in the AutoLookup property. But a developer can override the lookup to show a different list of columns for a particular form or control.
- Lastly, a user may want to filter the records shown in the lookup form. A developer can control this using the relations node, but again there may be specific forms where they would like to override the default filter.
Therefore, it is in these scenarios that you need to know how to override the lookup method in D365 forms.
An Example Of When You Need To Override A Lookup Form
Let’s walk through an example of overriding the lookup method in D365 forms. I have a form named ‘Vehicle service workbench’. On this form, the users add vehicle records to a grid. They specify a vehicle ID, make ID and model ID. After selecting the Make ID, I would like to filter the lookup of the model ID to ensure it only shows models relating to the make ID the user has already entered.
Right now, when I click the drop-down for the Model ID, I see the full list of models.
I will show you how to filter the lookup to show a subset of records.
Which Lookup Method To Override?
To start, we need to override the lookup method in D365 forms. There are however, a couple places we can do override the lookup method. We can do so on the form control itself. However this will only affect that particular form control. This is not the ideal place. If this same field is shown in multiple areas of this form, then I would need to override the lookup method in all of those places. Therefore, it is better to override the lookup method on the Data Source field.
Open the designer for your form. In the left pane, expand the Data Sources node.
Next, expand the Data Source that contains the field whose lookup method you wish to override. In my case, I need override the ModelID field’s lookup on the rsmVehicle Data Source.
Expand the ModelID node and select the Methods node. Right click on the Methods node and select Override>lookup.
The system will generate the lookup method and open a new window with this code.
[Form]
public class rsmVehicle extends FormRun
{
[DataSource]
class rsmVehicle
{
[DataField]
class ModelID
{
/// <summary>
///
/// </summary>
/// <param name = "_formControl"></param>
/// <param name = "_filterStr"></param>
public void lookup(FormControl _formControl, str _filterStr)
{
super(_formControl, _filterStr);
}
}
}
}
Filter What Records Show In The Lookup Form
In this section, we will override the lookup to change what records show. The call to ‘super(_formControl, _filterStr);’ causes the base functionality to work. In this case, the base functionality shows all records, and it shows the columns that are listed in the AutoLookup field group. We would like to change the lookup to only show records from the rsmModel table, that have a Make ID that match what we have already entered.
Enter the following code:
public void lookup(FormControl _formControl, str _filterStr)
{
//Specify the name of the table the lookup should show data from.
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(rsmModel), _formControl);
//Create a new query
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
//Specify the name of the table the lookup should show data from.
queryBuildDataSource = query.addDataSource(tableNum(rsmModel));
//Filter the data on the MakeId field on the rsmModel table.
queryBuildRange = queryBuildDataSource.addRange(fieldNum(rsmModel,MakeId));
//Only show records where the MakeID field matches the value in
//rsmVehicle.MakeId, on the currently selected record.
queryBuildRange.value(queryValue(rsmVehicle.MakeId));
//Specify which fields should be shown in the lookup form.
// field returned is the first field referenced
sysTableLookup.addLookupfield(fieldNum(rsmModel, ModelId));
sysTableLookup.addLookupfield(fieldNum(rsmModel, Description));
sysTableLookup.addLookupfield(fieldNum(rsmModel, IsServiceable));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
The code creates a new query object. It specifies that the data to be shown will come from the rsmModel table. And it filters the data in this table to only show records where the MakeID equals the value on the currently selected record.
Control What Columns Show In The Lookup Form
In the previous section, I showed you how to control what records show. However, often times when overriding a lookup, all you want to do is change what columns are shown in the lookup form.
Let’s pretend we do not want to show the ‘IsServiceable’ field in the lookup. Remove the line ‘sysTableLookup.addLookupfield(fieldNum(rsmModel, IsServiceable));’ from the code. Re-compile the code and verify that you now only see two columns shown in the lookup.
If you had only wanted to change the columns that are shown in the lookup, the method would have looked like this:
public void lookup(FormControl _formControl, str _filterStr)
{
//Specify the name of the table the lookup should show data from.
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(rsmModel), _formControl);
//Specify which fields should be shown in the lookup form.
//The field returned is the first field referenced
sysTableLookup.addLookupfield(fieldNum(rsmModel, ModelId));
sysTableLookup.addLookupfield(fieldNum(rsmModel, Description));
sysTableLookup.performFormLookup();
}
What Happens When The Make Is Not Specified?
Of course after all of this, there is one problem. What happens if the user does not specify the Make ID on the vehicle form? In this case, when the user clicks the drop-down form the model ID, they will see no records shown. This is because there are no records in the Model table where the Make ID is blank.
In order to fix this, we just need to make a small change to the code. Replace this line of code queryBuildRange.value(queryValue(rsmVehicle.MakeId)); with this:
if (rsmVehicle.MakeId != "")
{
queryBuildRange.value(queryValue(rsmVehicle.MakeId));
}
In the above code, the system checks to see if the MakeId field is set on the record. If it is, then it uses that to filter the records show in the lookup form. If the MakeID is not set, then the system does not filter the records.
Now, we can see that the lookup works as expected even when the MakeID field is not set.
Conclusion
In this article you learned how to override the lookup method in D365. First, we looked at what scenarios you would need to override the lookup method compared to using the automatic lookup functionality. Secondly, we looked at which lookup method to override in a form. Thirdly, we looked and how to filter the records that are shown in the lookup. Fourthly, we looked and how to control what columns are shown in the lookup form. Lastly, we looked at how to show all records in one scenario and a filtered set of records in another.
In the future, we can look at out to extend a lookup method. I hope you learned something new today.