Filter Customer Type field to display only Accounts in D365 V9.0 using JavaScript

Posted On December 25, 2018 by Clinton D'Mello Posted in  Tagged in

Introduction:

In this blog we will see how to filter customer type field in Dynamics 365 to only show Accounts/Contacts dropdown.

Implementation:

Step 1: For demonstration purpose we will filter to only show Accounts. In the below image we can see that we have  a Customer type field which allows to choose from Contacts as well as Accounts.

Note: Here accounts are called as Companies.

Step 2: To display only Companies in the dropdown we register an event on Form Load.

Here we are working on the Contacts form, hence we first write a JavaScript code to filter the lookup to show only companies. Below shown in the code:

Code:

var oContactFormCustomization = {

setCustomerLookupToShowCompany: function (execContext) {

var formContext = execContext.getFormContext();
if (formContext.getControl("parentcustomerid")) {
var company = formContext.getControl('parentcustomerid');

if (company.getEntityTypes().length > 1) {
company.setEntityTypes(['account']);
}
}
}};

We add this script in CRM JS Web resource and add the library on the Contact form Properties

Form Properties

Below shown is the Handler Properties

Handler Properties

Once this is done we can see when the form loads the scripts executes and we can now view only the Companies and not the Contacts

Company Lookup Only

Hope this helped!


Share Story :