Wednesday 20 May 2015

Customize Customer Lookup to show only Accounts or Contacts

Hi,
 
In some projects we may need customize the Customer look up on Case, Opportunity etc.. for showing only Accounts or Contacts based on some conditions.
 
We can achieve this using addPreSearch and addCustomFilter in JavaScript. Let us see this now.

I have taken Case entity here.

By default, Customer Look up shows Accounts and Contacts.

Accounts


Contacts


Let us customize it by writing some JavaScript code. The method is called on the load event, you can use it based on your requirement.

Here is the JavaScript code

function OnLoad() {
    addEventHandler();
}
 
function addEventHandler() {
    Xrm.Page.getControl("customerid").addPreSearch(addFilterToShowContacts);
    //Xrm.Page.getControl("customerid").addPreSearch(addFilterToShowAccounts);
}
 
// For showing only Contacts.
function addFilterToShowContacts() {
    var account_filter = "<filter type='and'>" +
     "<condition attribute='accountid' operator='null' />" +
    "</filter>";
    Xrm.Page.getControl("customerid").addCustomFilter(account_filter, "account");
}
 
// For showing only accounts.
function addFilterToShowAccounts() {
    var account_filter = "<filter type='and'>" +
    "<condition attribute='contactid' operator='null' />" +
    "</filter>";
    Xrm.Page.getControl("customerid").addCustomFilter(account_filter, "contact");
}
Showing Only Contacts




Showing Only Accounts



The only problem I see in this approach is User has select the dropdown(Account/Contact) but this is only supported way we have. Let's hope next versions of CRM will give some other technique to solve it for us.

Hope this helps.
--
Happy CRM'ing
Gopinath.

No comments:

Post a Comment