Lookup field on a Dynamics 365 form – add filter
Overview
This post describes how a supervisor at Contoso Pharmaceuticals Limited can be added to a particular clinical trial. One particular requirement is that only supervisors listed in the ‘resources pool’ sub grid (Figure 2) can be selected as a supervisor.
Detail
Initially, the Supervisor lookup control (Figure 2) displayed all resource records in the entity cpl_resource which have the Position = Supervisor.
![](https://dustinminer.com/wp-content/uploads/2021/12/Filter-4.png)
However, to meet the requirement, a second filter needed to be applied to this lookup so that it only displays resources from the ‘resources pool’ sub grid with the Position = Supervisor (Figure 2)
![](https://dustinminer.com/wp-content/uploads/2021/12/5aa-1.png)
First Approach – Related Records Filtering
To meet this requirement, I first tried filling out the ‘Related Records Filtering’ section of the Supervisor lookup control. Referring to Figure 3, note that ‘Resources (CTNumber)’ represents the ‘Resources Pool’ subgrid in Figure 2
![](https://dustinminer.com/wp-content/uploads/2021/12/3b.png)
This didn’t appear to work correctly. That is, ‘Test user1’ wasn’t the only supervisor appearing in the Supervisor lookup (Figure 4). (Note the Supervisor lookup control is defined as cpl_supervisorresource which is referenced in Figure 5. It’s a lookup to the cpl_resource entity)
![](https://dustinminer.com/wp-content/uploads/2021/12/5bb-1.png)
Second Approach – addPreSearch & addCustomFilter
The second approach was to add the JavaScript client API addPreSearch() & addCustomFilter() to the Supervisor lookup control. The filter only displays resources which are linked to the current clinical trial
![](https://dustinminer.com/wp-content/uploads/2022/06/JS-6-1.png)
This worked as shown below
![](https://dustinminer.com/wp-content/uploads/2021/12/5c-2.png)
A couple of things to note:
1.) One potential issue is that the browser may display previously entered values (e.g. Test user2 – Supervisor) for selection. To resolve this in the classic editor, select ‘Disable most recently used items for this field’
![](https://dustinminer.com/wp-content/uploads/2022/01/Cache1.png)
2.) If the error “0x80041103 Query Build Error” is generated (because of the link-entity in the fetch xml) when executing addCustomFilter(), then call addCustomView() instead.