Filtered lookup for Dynamics CRM 2013

Filtered lookup is a feature which was introduced in Dynamics CRM 2011, a greatly appreciated improvement from CRM 4. Prior to this, developers had to use third party tools to achieve the same result.

So, let’s start at the very beginning

What is filtered lookup?

When you have a large data set, and you want to filter the data in a progressive manner, then you need filtered lookup. You select one criteria/field and the data is filtered as per that. Now, if you choose the next field, then this filter is further applied to the already filtered data so as to give your further specific results.

For example: You have a list of prospective sales leads, and you want to filter out prior to a campaign based on those who are doctors and those who are in the state of Massachusetts. For this data selection, you would require a filtered lookup, so that the state/ profession is first selected and then following which on choosing the second criteria, the filter should be applied to the already once filtered data.

Is it the same for Dynamics CRM 2011 and 2013?

One of the main differences in filter view functionality in CRM 2013 is that you can pre-filter the columns currently in default view, which was not possible in CRM 2011. This works really well when you don’t have a complex query in the first place. An exception being, if your fetch XML has a link entity it will not work, because you can’t filter from columns that aren’t in the view yet.

Custom lookup control

Below is the screenshot of normal lookup of account from the contact form of CRM

Now let’s filter the lookup to show only those account records where Industry (an option set field on account) is set to Consumer Services.  (See the screenshot shown below)

Now, let us write the code to achieve this.

This piece of code is very small as compared to what we used to write to achieve the same result in CRM 2011.

function preFilterLookup() {
    debugger;
    Xrm.Page.getControl("new_linkedaccount").addPreSearch(function () {
        addLookupFilter();

    });
}
function addLookupFilter() {
    var fetchXml = '<filter type="and"><condition attribute="industrycode" value="8" operator="eq"/></filter>';
    Xrm.Page.getControl("new_linkedaccount").addCustomFilter(fetchXml);
}

NOTE:

1. addLookupFilter function can only be called from addPreSearch function that binds the function to the Lookup Control. Although, you can provide any filter condition in the fetchxml form (which you can get using advanced find) to see the filtered records.

2. Two parameters ‘filter’ and ‘entityLogicalName’ are what the addCustomFilter method will accept. If the entityLogicalName is provided (not mandatory), the filter will only be applied to that entity type, else it will be applied to all types of entities returned.

The following two tabs change content below.

Vishal Pandey

A Microsoft Dynamics CRM Developer who enjoys playing with technologies and a firm believer in the motto 'Knowledge is Power' !

Latest posts by Vishal Pandey (see all)

Vishal Pandey

A Microsoft Dynamics CRM Developer who enjoys playing with technologies and a firm believer in the motto 'Knowledge is Power' !

All stories by: Vishal Pandey