Bhoopathi

"Be Somebody Nobody Thought You could Be"

Tuesday, August 30

MS CRM :: Apply Custom Filter on Lookup Field

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.custom_filter
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. custom_filter1
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.custom_filter2
Function “filterLookup” will be trigger on change of field “Address1: City”. This binds “addPreSearch” event to lookup control “parentcustomerid”.custom_filter3
Open the contact entity record, Before entering the “Address1:City” field value the lookup field shows all the account records as below screenshot.custom_filter4
Enter the value for “Address1: City” here it is “US” as below.custom_filter5
Then Check for the suggested options for the Account lookup. Only those accounts records will be available to select which having city “US”.