How to Filter MS D365 CRM Lookup with Custom Conditions Using JavaScript

Filter MS CRM Lookup with Custom ConditionsFilter MS CRM Lookup with Custom Conditions

Microsoft Dynamics CRM is a popular customer relationship management platform that allows businesses to manage their customer interactions and relationships effectively. Lookups are an essential part of CRM, allowing users to search for and select records from related entities. By default, lookups display all records from the related entity, but sometimes you need to filter the results to show only specific records. In this article, we will discuss how to filter MS CRM lookup using JS in 2023.

Why filter a lookup using JS?

Filtering a lookup is useful when you want to limit the number of records displayed to users. This can help users find the specific record they need more quickly, rather than scrolling through a long list of records. JS is a powerful tool in MS CRM that allows you to customize the user interface and add functionality to the platform. With JS, you can easily filter lookups to show only the relevant records, saving time and improving the user experience.

Steps to filter MS CRM lookup using JS

Identify the lookup field that you want to filter

  1. The first step in filtering a lookup using JS is to identify the lookup field that you want to filter. You can do this by opening the form in the CRM application and looking for the lookup field on the form.

Get a reference to the lookup attribute and control

  1. Once you have identified the lookup field, you need to get a reference to the lookup attribute and control using the Xrm.Page.getAttribute() and Xrm.Page.getControl() methods.

Attach a function to the lookup control’s addPreSearch() event

  1. The addPreSearch() event is triggered just before the lookup dialog is displayed, allowing you to modify the lookup view. To attach a function to the addPreSearch() event, you can use the addPreSearch() method on the lookup control.

Add a filter to the lookup view using fetchXml

  1. To add a filter to the lookup view, you need to use fetchXml. FetchXml is a query language used in MS CRM to retrieve data from the platform. You can create a fetchXml query that filters the lookup view to show only the relevant records and use it to modify the lookup view.

Example code snippet:

coding

Here is an example code snippet that filters the “new_account” lookup field to show only accounts that have a specific value in the “new_industry” field:

function filterAccountLookup() {

   var accountLookup = Xrm.Page.getAttribute("new_account");

   if (accountLookup != null) {

      var accountLookupControl = accountLookup.controls.get(0);

      accountLookupControl.addPreSearch(function () {

         var fetchXml = "<filter type='and'><condition attribute='new_industry' operator='eq' value='Manufacturing' /></filter>";

         accountLookupControl.addCustomFilter(fetchXml, "account");

      });

   }

}

// Attach the function to the form’s OnLoad event

Xrm.Page.data.addOnLoad(filterAccountLookup);

In this example, we first get a reference to the “new_account” lookup attribute and control. Then we attach a function to the addPreSearch() event that creates a fetchXml query that filters the lookup view to show only accounts with “Manufacturing” in the “new_industry” field. Finally, we attach the function to the form’s OnLoad event.

Conclusion

Filtering a lookup using JS is a powerful tool in MS CRM that allows you to limit the number of records displayed to users and improve the user experience. By following the steps outlined in this article, you can easily filter MS CRM lookup using JS in 2023. Remember to test your code thoroughly before deploying it to production to ensure that it works as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *