Apply Custom Filter on Lookup Field in Dynamics CRM using Script
Introduction
In this blog we are going to see how to apply custom filter to the lookup field using the JavaScript functions.In Microsoft Dynamics CRM we can filter a lookup field on form using the Fetch XML condition and “addPreSearch()“ method.
Example
On the Contact Entity there is a lookup field named “Account Name” and a text field “Address1: City” as shown in below screenshot.
So, Here If we want to filter Account records in lookup view by city having value equal to field Address1: City. We can do this by writing the below code in Jscript.
Here we have written two functions “filterLookup()” and “addCustomeLookupFilter()” as shown in the below code snippets.
We have created CRM webresource for the javascript and called “filterLookup” function on change event for field “Address1: City” field as below for contact entity form.
Function “filterLookup” will be trigger on change of field “Address1: City”. This binds “addPreSearch” event to lookup control “parentcustomerid”.
Open the contact entity record, Before entering the “Address1:City” field value the lookup field shows all the account records as below screenshot.
Then Check for the suggested options for the Account lookup. Only those accounts records will be available to select which having city “US”.