Remove account or contact from customer lookup in Dynamics CRM
Microsoft Dynamics CRM 2015/2016 has a special type of lookup called customer lookup which can store both account and contact. Sometimes we are required to store either account or contact instead of both. Luckily we can do this! The SDK provides this methods called addPreSearch through this method we can achieve this behavior!
addPreSearch
Xrm.Page.getControl(arg).addPreSearch(handler)
This SDK says:
The argument is a function that will be run just before the search to provide results for a lookup occurs. You can use this handler to call one of the other lookup control functions and improve the results to be displayed in the lookup.
addCustomFilter
Xrm.Page.getControl(arg).addCustomFilter(filter, entityLogicaName)
The first argument filter is for fetchxml filter
The second argument entityLogicaName is optional if this is set, the filter only applies to that entity type. Otherwise, it applies to all types of entities returned.
To restrict the customer lookup to show only accounts, Use below JavaScript code! Add this code to a JavaScript library and add that library to the desired form and finally add filterCustomerLookup to form load event. That’s it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function filterCustomerLookup() { // To restrict accounts use below Xrm.Page.getControl("customerid").addPreSearch(restrictAccounts); // To restrict contacts use below // Xrm.Page.getControl("customerid").addPreSearch(restrictAccounts); } function restrictContacts() { var customerAccountFilter = "<filter type='and'><condition attribute='contactid' operator='null' /></filter>"; Xrm.Page.getControl("customerid").addCustomFilter(customerAccountFilter, "contact"); } function restrictAccounts() { var customerAccountFilter = "<filter type='and'><condition attribute='accountid' operator='null' /></filter>"; Xrm.Page.getControl("customerid").addCustomFilter(customerAccountFilter, "account"); } |
How the restrictContacts works?
We are using fetchxml filter and it applies on contact entity. The filter says:
Retrieve only those contacts which has contactid null, but the contactid is a primarykey for contacts and it cannot be null. So there will be no contacts in CRM which can fulfill this criteria and so it will return no contacts.
How the restrictAccounts works?
We are using fetchxml filter and it applies on account entity. The filter says:
Retrieve only those accounts which has accountid null, but the accountid is a primarykey for accounts and it cannot be null. So there will be no accounts in CRM which can fulfill this criteria and so it will return no accounts.
In our example we have restricted the customer lookup to contacts only and here are the results! The customer lookup is listing only contacts.
For more help related to MS Dynamics CRM, Please Contact Us.
Leave a Comments