How to Dynamically Filter Lookups in Microsoft Dynamics CRM

Posted By
BDO

One function of Microsoft Dynamics CRM is the ability to dynamically filter records, making it easier to sort through campaigns based on keywords.

In this scenario, I want to limit which records appear in the Campaign lookup view based on the value of the Opportunity Type option set. This approach is only valid for updated entities in Microsoft Dynamics CRM 2013 and later, i.e., all custom entities and some system entities.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

How to Filter Lookups in Microsoft Dynamics CRM

The first step is to create a JavaScript function that specifies the filter. The filter condition can change based on the Opportunity Type value, as shown in the example below. If the user picks “Dept A”, then only campaigns starting with “RED” will appear in the lookup view. Note that sample FetchXML can be generated by creating an Advanced Find in CRM, then downloading the code.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

The next step is to call this function from the addPreSearch method in the Form_onLoad event if the Opportunity Type matches one of the options we are using for filtering.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

We must also call this function from the Opportunity Type field change event. The code is the same as for the onLoad event, but an extra line is added for the default case. This removes the filter if the user chooses a different (or blank) option. Notice that the removePreSearch method also calls the FilterCampaignLookup function.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

If you are using Microsoft Dynamics CRM 2016 and have “Use Legacy Form Rendering” checked in the system settings, you may get an error. In this scenario, I added the following line of code before the addPreSearch and removePreSearch lines.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

Now when I choose “Dept A” as the Opportunity Type, only Campaigns starting with “RED” appear.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

If I click on “Look Up More Records”, the same filter will apply. It also applies if I change the lookup view.

How to Dynamically Filter Lookups in Microsoft Dynamics CRM

Visit BDO IT Solutions for more information or contact the CRM team directly.

BDO eBook - The Cloud Changes the Game

There are times in the course of your business when you have the opportunity to dramatically accelerate growth and improve day-to-day efficiencies. Recognizing

Download