Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Restrict Customer Lookup to Accounts or Contacts in Dynamics CRM

Post Author: Joe D365 |

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!

Blog Image

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 = "";

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!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

9 comments on “Restrict Customer Lookup to Accounts or Contacts in Dynamics CRM”

  1. Hi Joe,

    the contact records is gone,but the lookup default view is still the contact, I have to switch to the account view and then choose the account record,How can I set the account view to be default?

      1. Contact me at "amer dot zec at compusight dot com" and I will provide you with details how to get this done..

        1. Hi Amer, Thank you for replying, but I thought the purpose of these discussion forums was to share knowledge, so why not just post your answer here? I did actually find a way to set the default which was to add attribute 'defaulttype' to the underlying control and set its value to 1. It is an unsupported method but works, however, if you have a better method...

  2. Something I couldn't find mentioned anywhere, maybe it can help someone:

    If you are filtering a polymorphic field like "customerid", which can be either an Account or Contact, you can add a different fetchXml filter for each entity:
    Xrm.Page.getControl(“customerid”).addCustomFilter(accountFilter, “account”);
    Xrm.Page.getControl(“customerid”).addCustomFilter(contactFilter, “contact”);

    So you could not only filter out contacts as shown above, but also apply a separate filter to the remaining accounts. This was tested working in 2016 on prem.

  3. Hey Joe, how do you make this work with the form in the header (the bit in the workflow process) when you are creating a new (eg Case) entity?

  4. I tried this code. Is this code shows only Account or only contact in the entity list?
    Because with this code we can set the default entity selected in the lookup entity but still contact entity option is available if i want to change the lookup

PowerObjects Recommends