Restricting the customer lookup

EDIT (07/03/2018)Updated the post for v9. In v9, you can use the supported approach of setEntityTypes and getEntityTypes. Refer https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/controls/setentitytypes

EDIT (04/07/2017): After testing this in 8.2.1, I have simplified the code as getLookupTypes and setLookupTypes can be used straight from the lookup itself, instead of going through getLookupDataAttribute.

First off, I have tested this only in CRMOnline on 8.1.0 and 8.2.1 so this might not work for you if you are are in older version. You can now easily restrict the default entity of the lookup, if it links to multiple types. e.g. customer lookup. The script below is for attribute of type “customer”, but you can follow a similar approach for “partylist” as well.

These new functions are undocumented, so they are technically unsupported (only 8.x. 9.x is documented and supported). So, use this at your own risk. In the script below, I am restricting a field with schemaname “customerid” and of type “customer” to “contact” entity only.

JavaScript (9.x) – Supported

var lookup = Xrm.Page.getControl('customerid');

//check if multiple type dropdowns enabled for this lookup
if (lookup.getEntityTypes().length > 1) {
    lookup.setEntityTypes(['contact']);
}

JavaScript (8.x) – Unsupported

var lookup = Xrm.Page.getAttribute('customerid');

//check if multiple type dropdowns enabled for this lookup and it is not a partylist. For partylist we might want to select an account and a contact
if (lookup.getLookupTypes().length > 1
     && !lookup.getIsPartyList()) {
    lookup.setLookupTypes(['contact']);
}

Screenshots

LookupLookup Search Customer

You can use this script during form load and easily restrict the lookup type.

23 comments

  1. Thanks for posting this. When I use the Customer field on the opportunity form I usually want it to refer to the Account, and it annoys me that when I type the account name it returns all the contacts at the account. This solution will make it easier to restrict the results to just accounts when needed.

    • It returns type of currently selected lookup. If a lookup can be account or contact, and the current value of the lookup is account, this method will return 1 (entitytypecode for account).

  2. Hi, get an error when applying this to CRM Online 2016 Case form. Object doesn’t support property or method ‘getLookupDataAttribute’

    Please advise

  3. Hi I need similar functionality for CRM for Outlook plugin.
    Where should I enter this java script code in this case?
    Thanks.

  4. This is great. One small problem though.
    When I apply this for accounts as I only want accounts, it works great when I click the “Lookup Up More Records” I can only select Accounts.
    But, if I click the lookup icon, and then click “New” button (without clicking Lookup Up More Records), then the Quick Create form for Contact appears.

  5. You are correct Alano. Since this function is not even documented, it is technically unsupported. To me, it appears to be a bug, which might be fixed if this function is officially released and documented..

  6. Thanks you Natraj,

    I figured out a different way to do this. Just replace the Customer lookup on the form with the Account lookup.

    What I had not realised was that since CRM 2015, the customerid field has two linked fields, Account and Contact. If you update the Account or Contact field, then the Customer field is updated with a matching value. This also works the other way around, if you Customer field, then the Account or Contact fields are updated accordingly.

    This is a solution to my requirement.

  7. Is there a way to determine the order in which the lookups are displayed?
    I need for customer both the contact and account, both the contact as default.

  8. By default it will be the view that ends with “Lookup view”. You should be able to edit that from the solution. You can also choose a custom view and the default lookup view from the solution as well.

  9. Thanks for this Natraj. Based on your blog i was able to set the order of the types and also set the default view of the 1st type by using the following code

    var lookup = Xrm.Page.getAttribute(‘to’);

    if (lookup && lookup.getLookupTypes().length > 1) {
    lookup.setLookupTypes([‘contact’, ‘account’, ‘lead’, ‘systemuser’, ‘queue’, ‘entitlement’, ‘equipment’, ‘knowledgearticle’]);

    var lookupcontrol = Xrm.Page.getControl(‘to’);
    if (lookupcontrol) {
    var id = “{837C57F9-833C-E611-80EC-6C3BE5A8C804}”; // my custom view guid
    var defaultView = lookupcontrol.getDefaultView();

    if (defaultView !== id) {
    lookupcontrol.setDefaultView(id);
    }
    }
    }

  10. Would this solution still work on the “convert email to case form”? Customer is required on this form.

Leave a comment