Limit customer lookup to show only accounts or contacts

Entities like opportunity, case, order and invoice comes out of the box with a customer look that allows users to select between accounts and contacts. It is a very common request from customers to allow users to select only accounts or contacts. In CRM 2011 days, we used to set the defaultlookuptype to 1 for account and 2 for contact but this logic does not work with CRM 2013. With introduction of new client side scripting methods, we can use addPreSearch method to achieve this. In this example, I have called addEventHandler method on load of case form so customer lookup will only show accounts.


function addEventHandler() 
{
    Xrm.Page.getControl("parentcustomerid").addPreSearch(addFilter);
}

function addFilter() 
{
    var account_filter ="<filter type='and'>" +
    "<condition attribute='contactid' operator='null' />" +
    "</filter>";
    Xrm.Page.getControl("parentcustomerid").addCustomFilter(account_filter, "contact");
}

You will see that the filter I used is contactid = null, this is only possible for accounts as contacts will always have their primary key populated.

5 thoughts on “Limit customer lookup to show only accounts or contacts

Add yours

  1. You can definitely see your expertise within the article you write.
    The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe.

    All the time go after your heart.

  2. If I understand your example code, it prevents Contacts from appearing as opposed to allowing Accounts. In the case of a Customer lookup field, the result is the same, but what about for a Regarding field? If you only want to see accounts in a regarding field, following your code we would have to exclude every other entity type within CRM.

    Rather than use “”, how about using “” ?

  3. Hi, thanks for the example. Should I create a custom JS Library on CRM and add this as a function and then later call the function addEventHandler on the ONLOAD event of the form?

  4. Hi ashishkhanna,

    I’m trying to do the same as you said.

    I only want to show Accounts in “customerid” lookup field in Case Quick create form.

    However, when I click the lookup, there is no results and lookup field keep thinking.

    This is my code

    function addEventHandler() {
    Xrm.Page.getControl(“customerid”).addPreSearch(Infoavan.Incident.addFilter());
    },

    function addFilter() {
    var account_filter = “” +
    “” +
    “”;

    Xrm.Page.getControl(“customerid”).addCustomFilter(account_filter, “contact”);
    },

    Best Regards. Thanks

Leave a reply to how to Cancel reply

Create a website or blog at WordPress.com

Up ↑