The post describes an example of how to filter the results displayed in a Dynamics 365 lookup control using the client API addPreSearch & addCustomFilter
Scenario
A supervisor at Contoso Pharmaceuticals Limited can be added to a particular clinical trial (via the Supervisor lookup in Figure 1). The lookup control already has a filter applied to it (Figure 2) however, a second filter needs to be applied so that it’s only possible to add Supervisors that are listed in the Resources Pool (Figure 1)
Implementation
The JavaScript displayed in Figure 3 applies addPreSearch() & addCustomFilter() to the Supervisor lookup control. The filter only displays resources which are linked to the current clinical trial
When the JavaScript is run, the lookup filter works correctly as illustrated in Figure 4
Appendix
A couple of things to note:
1.) The browser may display previously entered values (e.g. Test user2 – Supervisor) for selection in the lookup. To resolve this in the classic editor, select ‘Disable most recently used items for this field’ (Figure 5)
2.) If the error “0x80041103 Query Build Error” is generated (because of the link-entity in the fetch xml) when executing addCustomFilter(), then use addCustomView() instead.
Further reading
Xrm.Utility.lookupObjects to add records to a subgrid
Get and set a Dataverse lookup data type column