Thursday, October 24, 2013

addCustomFilter method for lookup control in CRM2013

In CRM2011, Microsoft introduced a new method “addCustomView” to filter the lookup dialogs.  The process was a bit complicated. You have to create a unique guid, fetchxml, layoutxml and few other things.
In the new release, Dynamics team has introduce a new method addCustomFilter. It makes it very easy to apply filters to the lookup dialog. But there is one gotcha.This method can only be called from a “PreSearch” event handler of the lookup control. This event get triggered as the user is about to view results for the lookup.
We can add and remove the event handler to the PreSearch event using addPreSearch(handler) and removePreSearch(handler) methods.
In this blog, I am going to filter the parent customer lookup on the contact entity based on the city of the contact. Here is the code.


function addEventHandler() {
    // add the event handler for PreSearch Event


function addFilter() {
    //check if the city is not empty
    if (Xrm.Page.getAttribute("address1_city").getValue() != null) {
        var city = Xrm.Page.getAttribute("address1_city").getValue();
        //create a filter xml
        var filter ="<filter type='and'>" +
                     "<condition attribute='address1_city' operator='eq' value='" + city + "'/>" +
        //add filter


I am calling the addEventHandler function on the form load event.
The addEventHandler() function is used to attach the event handler to the PreSearch event of the “parentcustomerid” lookup of contact entity.
The addFilter() function will be called when the PreSearch event is triggered.

Screen Shot

In the screen shot above, the lookup is displaying accounts and  contacts where city is equal to ‘Renton’

Things to remember

addCustomFilter method takes two parameters ‘filter’ and the ‘entityLogicalName’. The entityLogicalName is optional and if this parameter is provided, the filter will only apply to that entity type. Otherwise it will apply to all types of entities returned.

For e.g. customer lookup control display account and contact records. If we don’t provide entityLogicalName  parameter, the filter will apply to both account and contact records and if we provide “account” as a  parameter then filter will only be applied to account records not to contact records.


  1. Doubt that works. city value not used anywhere.


    1. thanks mate. the code does work. I forgot to encode the XML.

  2. is it possible to use this method to limit a lookup to an entity, I.e the customerid on the opportunity to just a contact and not the account as well?


  3. Hi Pete, did you fix the issue to limit the lookup custom filter to return just one entity type?

  4. Hi Amreek, is there any way to restrict or default CustomerID lookup only to contacts using supported way?

  5. Is there any way to specify columns

  6. Thank you! This solution works in the classic interface in Dynamics 365 but not in the Unified interface. Could you please let me know if there is a way to make this work in Unified interface in Dynamics 365?