I recently wanted to filter a Lookup within Dynamics 365 to show Contacts where the linked Accounts’ Parent Account was a specific Account. The idea being that the user can select any Contact from the Account Hierarchy but shouldn’t be able to select any Contacts from outside of that structure.

Normally, I would use addCustomFilter & addPreSearch to perform this action but I was receiving an error regarding the linked entity not existing. A bit of research suggests that the preFilter cannot have a linked entity included.

Whilst researching this functionality, I found the addCustomView function which allows a custom view to be created using fetchXml and layoutXml (a tip on getting these is here). This can then be assigned to the lookup and used as a filter to only show specific records.

Account Structure

In this example, I am using the Accounts & Contacts in the image below. I will be viewing the Parent Account or Child Account records and will be expecting only users assigned to either of these show in the lookup.

Contacts in Dynamics
View showing the contacts within Dynamics 365


As would be expected, without including any custom filters all of the contacts are shown when trying to use the lookup.

Lookup showing all accounts before the custom view is applied
All accounts showing as no filter in place


Adding the below JavaScript and running the function on Page Load will apply the custom view. In this example, I have hardcoded the GUIDs the ID of the Parent Account.


After publishing the changes & reloading the page, you can now see that the Contact lookup is filtered to only show the the Contacts linked to the Parent Account.

Lookup filtered to show items in account hierarchy
Lookup showing only accounts within the parent child structure


This was an interesting way of getting around the preFilter not accepting a linked entity and was easy to add into the solution. One thing to note is that this will not prevent items showing in the ‘Recents’ view for the lookup, so I would recommend disabling that too.

The post Using addCustomView with Lookups appeared first on Jason Clair.