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.

Leave a Reply

Your email address will not be published. Required fields are marked *