Linkedin
YouTube
Skype

Dynamically filter Account and Contact Lookups Based on Type

Quick Inquiry

captcha

Client Speaks

Client Speaks - Testimonials for Aegis Software
You Are @ >> Home >> Articles >> Dynamically filter Account and Contact Lookups Based on Type
How to dynamic filtering lookup fields in Microsoft dynamics CRM

DESCRIPTION

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 OptionAccount Type OptionInteger Value
1Family MemberSupport Staff100000000
2Franchise ContactFranchise Unit100000001
3Vendor Contact Vendor100000002

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

STEPS

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

UNIT-TESTING

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

CONCLUSION

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.

do not copy