Tip to Filter Customer Field depending on ‘Type of Customer’ in Dynamics 365 UCI

By | October 1, 2019

Introduction

Recently, we faced an issue in UCI where we had to filter customer field depending on Type of customer.

Here, the requirement was as follows:

If the type of customer is account then the customer field should show Account entity records only.

If the type of customer is contact then the customer field should show Contact entity records only.

For this, we had an option set field named ‘Type of customer’ which contains ‘contact’ and ‘account’ option.

We had added a JavaScript function to set entity type according to value of ‘Type of customer’ field using setEntityTypes function as shown in following code:

 function onLoad(executionContext) {
    try {
var formContext= executionContext.getFormContext();
formContext.getControl("customerid").addPreSearch(filterCustomerLookup);
    } catch (e) { }
}

function filterCustomerLookup(executionContext) {
    try {
        	var formContext= executionContext.getFormContext();
        if (formContext.getAttribute("new_customertype").getValue() == 100000001) {
            formContext.getControl("customerid").setEntityTypes(["account"]);
	} else {
            formContext.getControl("customerid").setEntityTypes(["contact"]);
         }
    } catch (e) { }
}

This function was working in Classic web UI, but on UCI, the customer was not being filtered correctly.
For example, in our case, ‘contact’ was default value of ‘Type of customer’ and when we were changing ‘Type of customer’ field value from Contact to Account and clicking on search button of Customer field we were getting Contact records instead of Account records.

Shown in below screenshot is the Records in customer lookup on selecting customer Type Contact:

Customer Field

Shown in below screenshot is the Records in customer lookup on selecting customer Type Account:

Customer Field

So, we removed the pre-search and tried using below function on Load and on Change of ‘Type of Customer’ field.

function onChangeOfTypeOfCustomer(executionContext) {
    try {
var formContext= executionContext.getFormContext();
        if (formContext.getAttribute("new_typeofcustomer").getValue() == 100000001) {//100000001 value if Account option in Type of customer field
            formContext.getControl("customerid").setEntityTypes(["account"]);
        } else {
            formContext.getControl("customerid").setEntityTypes(["contact"]);
        }
    } catch (e) {   }
}

Again these changes were working in Classic web UI but on UCI, customer was being filtered only for the default entity type.
For example, in our case contact was default value of ‘Type of customer’ and when we clicked on new ‘Type of customer’ field value was being set to Contact by default. As a result, only contact entity was set in Customer field entity types using following code:
formContext.getControl(“customerid”).setEntityTypes([“contact”]);
We were getting records of contacts as shown below:

Customer Field
But, when we were changing ‘Type of customer’ field value to ‘Account’, it was not showing any account record and keeps showing ‘Loading’ text forever, even though we set account entity in Customer field entity types using following code.

formContext.getControl(“customerid”).setEntityTypes([“account”]);

Customer Field

On further checking, we observed that this was due to function on ‘onLoad’ event was directly setting entity type.

So for handling customer type on load event, we had to combine the first original code with changed code.

The final working solution is as below:

Function on Change event of Customer Type field:

function onChangeOfCustomerType(executionContext) {
try {

var formContext= executionContext.getFormContext();

if (formContext.getAttribute("new_customertype").getValue() == 100000001) {
formContext.getControl("customerid").setEntityTypes(["account"]);
} else {
formContext.getControl("customerid").setEntityTypes(["contact"]);
}

} catch (e) {

}

}

Function on “onLoad” event of form:

 function setCustomerTypeOnLoad(executionContext) {
    try {
        var formContext= executionContext.getFormContext();
        formContext.getControl("customerid").addPreSearch(filterCustomerLookup);
    } catch (e) {    }
}
function filterCustomerLookup(executionContext) {
try {
var formContext= executionContext.getFormContext();
if (formContext.getAttribute("new_customertype").getValue() == 100000001) {
formContext.getControl("customerid").setEntityTypes(["account"]);
} else {
formContext.getControl("customerid").setEntityTypes(["contact"]);
}
} catch (e) { }
}

Conclusion

Thus, with the above code you can easily filter customer field in Dynamics 365 UCI depending upon Type of Customer.

70% of global 2000 companies apply gamification to improve productivity and returns!

Gamifics365 – Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!

3 thoughts on “Tip to Filter Customer Field depending on ‘Type of Customer’ in Dynamics 365 UCI

  1. Usman

    Hi,
    Please confirm above JavaScript will work on CRM 2016 on Pram?

    1. Inogic

      Hi,

      The code in the blog works for Dynamics 365 UCI.

      For CRM 2016 On-Premise environment, you can use the below code which will allow you to not show any records other than your selected customer type :

      function setCustomerTypeOnLoad() {
      try {
      Xrm.Page.getControl(“customerid”).addPreSearch(filterCustomerLookup);
      } catch (e) { }
      }

      function filterCustomerLookup() {
      var contactFilter = ““;
      var accountFilter = ““;
      try {
      if (Xrm.Page.getAttribute(“new_customertype”).getValue() == 100000001) {//For Customer Type Account
      Xrm.Page.getControl(“customerid”).addCustomFilter(contactFilter, “contact”);
      } else {//For Customer Type Contact
      Xrm.Page.getControl(“customerid”).addCustomFilter(accountFilter , “account”);
      }
      } catch (e) { }
      }

      Thanks!

Comments are closed.