Dynamically filter Account and Contact Lookups Based on Type

How to dynamic filtering lookup fields in Microsoft dynamics CRM


In this blog, we will learn how we can dynamically filter lookup columns in Dynamics 365 Customer-Engagement based on a selected choice (option) in an option-set field. Consider an option-set called Contact Type having the following options (choices). The Parent Contact and Account Name (Parent Company) lookup fields must be filtered as per the following table.

S.No. Contact Type Option Account Type Option Integer Value
1 Family Member Support Staff 100000000
2 Franchise Contact Franchise Unit 100000001
3 Vendor Contact Vendor 100000002

We’ll make use of the addPreSearch and addCustomFilter JS functions to achieve the above programmatically.


1. We create a new JavaScript Web-Resource with the following code.

function filterParentAccountAndContactLookups(executionContext) { //To filter the display results in Parent Contact and Account Lookups based on Contact Type var formContext = executionContext.getFormContext(); formContext.getControl("parentcustomerid").setEntityTypes(["account"]); if (formContext.getAttribute("wb_contacttype").getValue() != null) { formContext.getControl("parent_contactid").addPreSearch(filterContacts); formContext.getControl("parentcustomerid").addPreSearch(filterAccounts); function filterContacts() { var contactType = formContext.getAttribute("wb_contacttype").getValue(); var contactFilter = "<filter type='and'><condition attribute='wb_contacttype' operator='eq' value='" + contactType + "'/></filter>"; formContext.getControl("parent_contactid").addCustomFilter(contactFilter, "contact"); } function filterAccounts() { var contactType = formContext.getAttribute("wb_contacttype").getValue(); var accountFilter = "<filter type='and'><condition attribute='wb_accounttype' operator='eq' value='" + contactType + "'/></filter>"; formContext.getControl("parentcustomerid").addCustomFilter(accountFilter, "account"); } } }

2. Add this library function On-Load of Contact main form and On-Change of Contact Type Field.

Microsoft dynamics CRM


1. When Contact Type is Family Member:

Parent Contact Lookup

Microsoft dynamics CRM1

Account Name Lookup

Microsoft dynamics CRM2

2. When Contact Type is Franchise Contact:

Parent Contact Lookup

Microsoft dynamics CRM3

Account Name Lookup

Microsoft dynamics CRM4

3. When Contact Type is Vendor Contact:

Parent Contact Lookup

Microsoft dynamics CRM5

Account Name Lookup

Microsoft dynamics CRM6


Thus, by Microsoft dynamics CRM development making use of the addPreSearch and addCustomFilter JS functions, we succeeded in developing a mechanism to dynamically filter the Parent Contact and Parent Company lookups based on the selected Contact Type option.