Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365 v9.x

By | November 16, 2018

Introduction:

Recently we had a business requirement where client wants to filter PartyList field by selected account in regarding field on activity entity form.

As shown in below image, when we click on party list field, by default it shows data of many entities (i.e. Account, Contact, and User etc.)

Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365

Our client requirement is to filter PartyList field “Required Attendees” and show only Contact based on Account selected in “Regarding” field and that should be supported in UCI.

To achieve this we have developed JavaScript, which apply addPreSearch to PartyList field “Required Attendees”.

Here we used “addCustomFilter” function to apply custom condition and “setEntityTypes” function to set entity of contact type.

For reference please find JavaScript code as below:

function filterRequiredAttendees() {
    
    var functionName = "filterRequiredAttendees: ";
    
    try {

        // Get requiredattendees
        var regardingObject = Xrm.Page.getAttribute("regardingobjectid");

        // Validate regarding field
        if (!isValid(regardingObject))  {
            return;
        }

        // Validate value of regardingObject
        if (regardingObject.getValue() == null) {
            return;
        }

        // Get requiredattendees control
        var requiredAttendeesControl = Xrm.Page.getControl("requiredattendees");

        // Validate requiredattendees field
        if (isValid(requiredAttendeesControl)) {

            // Add PreSearch
            requiredAttendeesControl.addPreSearch(filterContactByAccount);

            // Check if multiple type dropdowns enabled for this lookup
            if (requiredAttendeesControl.getEntityTypes().length >= 1) {

                // Set entity type to contact
                requiredAttendeesControl.setEntityTypes(['contact']);
            }
        }
    }
    catch (e) { throwError(e, functionName); }
}

function filterContactByAccount() {

    var functionName = "filterContactByAccount: ";

    try {

        var regardingObjectValue = Xrm.Page.getAttribute("regardingobjectid").getValue();

        // Validate Regarding has a value
        if (regardingObjectValue != null) {

            // Get Account GUID
            var regardingID = regardingObjectValue[0].id;

            var filterCondition = "<filter type='and'>" + "<condition attribute='parentcustomerid' operator='eq' value='" + regardingID + "' />" + "</filter>";
            Xrm.Page.getControl("requiredattendees").addCustomFilter(filterCondition, "contact");

        }
    }
    catch (e) { throwError(e, functionName); }
}

Add this scripts as web resources, add these libraries to the activity entity form, then we have to set the Form Properties, set “filterRequiredAttendees” function “OnLoad” of the form, and “OnChange” of the “Regarding” field.

Deploy and publish the changes. Refresh (Ctr+F5) the activity entity form and we are good to filter partylist field.

Now PartyList field “Required Attendees”, shows only Contact based on Account selected in “Regarding” field.

Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365

Conclusion:

Using the “addPreSearch”, addCustomFilter”, and “setEntityTypes” functions we can filter PartyList fields that supports on UCI too.

Dynamics 365 for Customer Engagement apps version 9.x provided the following client APIs, which are not available in the previous versions.

1. getEntityTypes – Gets the types of entities allowed in the lookup control

Syntax: formContext.getControl(arg).getEntityTypes();

2. setEntityTypes – Sets the types of entities allowed in the lookup control.

Syntax: formContext.getControl(arg).setEntityTypes([entityLogicalNames]);

Effectively-Manage-Sales-Territories-on-a-map-within-Microsoft-Dynamics-CRM

Leave a Reply

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