Microsoft Dynamics CRM 2015 comes with customer lookup on entities like opportunity, case, order, invoice and quote out-of-the-box. This lookup resolves to account and contact by default, but sometimes you may have a need to show either accounts or contacts in this view. In earlier versions of CRM, you could achieve this by writing a script on the lookup control to define the default lookup type to 1 for account and 2 for contact, however, this method has been deprecated and a new method called addPreSearch has been added to the SDK. In today’s blog, we will explain how to use addPreSearch to show either accounts or contacts in the customer lookup. Let’s begin!
Example: You want to show only accounts on the customer lookup field on the order entity. Add the below java script library to the form and then add the defaultcustomer method to the onload event on the form.
function defaultcustomer(){
Xrm.Page.getControl(“customerid”).addPreSearch(addFilter);
}
function addFilter()
{
var customerAccountFilter = “<filter type=’and’><condition attribute=’contactid’ operator=’null’ /></filter>”;
Xrm.Page.getControl(“customerid”).addCustomFilter(customerAccountFilter, “contact”);
}
Explanation: When you call the defaultcustomer, the system automatically attaches the addFilter method to the search criteria of the lookup control. In the filter condition of the addfilter method, we have mentioned that contactid equals null. contactid can only be null for accounts, as contactid is a primary key for contacts and cannot be null. If you want to display only contacts in the lookup then you can change the condition to be accountid equals null. After adding this script, the user will only be able to select an account in the customer lookup.
That’s all for the blog today, Remember to subscribe to our blog so that you can stay up to date on all the tips and tricks we post. And as always, happy CRM’ing!