Lookup field on a Dynamics 365 form – add filter


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.


Initially, the Supervisor lookup control (Figure 2) displayed all resource records in the entity cpl_resource which have the Position = Supervisor.

Figure 1

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)

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

Figure 3

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)

Figure 4

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

Figure 5

This worked as shown below

Figure 6

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’

Figure 7

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.