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.
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.
As would be expected, without including any custom filters all of the contacts are shown when trying to use the lookup.
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.
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, information about how to do that is in this post.