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 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.
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.
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.
Now when I choose “Dept A” as the Opportunity Type, only Campaigns starting with “RED” appear.
If I click on “Look Up More Records”, the same filter will apply. It also applies if I change the lookup view.