Restricting the customer lookup

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. 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

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.

Advertisements

19 thoughts on “Restricting the customer lookup

  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.

  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. 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.

  4. 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..

  5. 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.

  6. 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.

  7. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s