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.
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)
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
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)
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
This worked as shown below
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’
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.