Apply Custom Filter on Lookup Field in Dynamics CRM using Script

By | August 20, 2015

Introduction:

In this blog, we are going to see how to apply a custom filter to the lookup field using the JavaScript functions.

Microsoft Dynamics CRM allows us to filter a lookup field on the 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 the below screenshot;Apply Custom Filter on Lookup Field in Dynamics CRM using Script

So, 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;Apply Custom Filter on Lookup Field in Dynamics CRM using Script

We have created CRM webresource for the javascript and called ‘filterLookup’ function on change event for the field ‘Address1: City’ field as below for contact entity form.Apply Custom Filter on Lookup Field in Dynamics CRM using Script

Function ‘filterLookup’ will be triggered on the change of field ‘Address1: City’. This binds ‘addPreSearch’ event to lookup control ‘parentcustomerid’.Apply Custom Filter on Lookup Field in Dynamics CRM using Script

Open the contact entity record, Before entering the ‘Address1:City’ field value the lookup field shows all the account records as below screenshot;Apply Custom Filter on Lookup Field in Dynamics CRM using Script

Enter the value for ‘Address1: City’ here it is ‘US’ as below;Apply Custom Filter on Lookup Field in Dynamics CRM using Script

Then Check for the suggested options for the Account lookup. Only those accounts records will be available to select which have the city as ‘US’.

Hope this helps!

Microsoft Dynamics CRM and QuickBooks Integration

2 thoughts on “Apply Custom Filter on Lookup Field in Dynamics CRM using Script

  1. tdailey77

    Hello,
    Can you help me with the same but on Phone Call form.

    The “Call To” field – we would like to filter that lookup to only show contacts that work for the Account that is in the “regarding” of the Phone Call record. For example, I go to Account ABC and add a phone call activity. The “Call To” lookup when I click on the magnifying glass should only show contacts that work for Account ABC. Possible? Can you show me the code and process to achieve this? Thank you

Leave a Reply