Skip to main content

Notifications

Announcements

No record found.

Filter Subgrid in Dynamics 365 CRM (Version 9.2)

For a task in my current project, a requirement was to show only those records in a subgrid for a custom entity “Compliance Item Regardings” on the Task entity form. There was no direct relationship available between the Task entity with the mentioned custom entity. So, in order to implement this, I did some settings on the subgrid properties window. Further, I had written a JS code to apply my FetchXML query on the subgrid to show only those records in the subgrid which are linked with regarding lookup field on the Task form.

First, add a subgrid on the task form and select “All Record Types” under the “Data Source” area in the properties window as shown in the screenshot. And then select your required entity in the next dropdown & then select Default View from the next dropdown.

Before moving forward, please note these things about the key points about this implementation.

Note: In my case subgrid id = subgridComplianceItemRegardings

Lookup field on the Task form has id = regardingobjectid

You can change these Ids as per your controls/fields Ids. Further, you can create your required FetchXML query as per your needs.

Then create a JS file and use the following code in that file.

var Task = Task || {};

Task.SubGridFilterExecution = function (executionContext) {
    var regardingId = '';
    var regardingEntityType = '';

    //Create a Form Context.
    var formContext = executionContext.getFormContext();
    //Step 1 - Get the subgrid control.
    var gridControl = formContext.getControl("subgridComplianceItemRegardings");
    //Step 2 - Retrieving Form Attribute Value.
    var regardingobjectid = formContext.getAttribute("regardingobjectid").getValue();
    if (regardingobjectid != null && regardingobjectid != undefined) {
        regardingId = regardingobjectid[0].id;
        regardingEntityType = regardingobjectid[0].entityType;
    }    

    //Step 3 - Recall the execution method if the subgrid context is null or empty.
    if (gridControl === null) {
        setTimeout(SubGridFilterExecution, 3000);
        return;
    }
    else {
        //Set grid with query A based fetch XML.
        if (regardingId !== null && regardingId !== '' && regardingEntityType === 'sc_complianceitem') {
            //Step 4 - Build a fetch XML in a variable.
            gridControl.setVisible(true);
            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                "<entity name='sc_complianceitemregarding'>" +
                                    "<attribute name='sc_name' />" +
                                    "<attribute name='sc_complianceitem' />" +
                                    "<attribute name='sc_contactrole' />" +
                                    "<attribute name='sc_complianceitemregardingid' />" +
                                    "<order attribute='sc_name' descending='false' />" +
                                    "<filter type='and'>" +
                                        "<condition attribute='sc_complianceitem' uitype='sc_complianceitem' operator='eq' value='"+regardingId+"' />" +
                                    "</filter>" +
                                "</entity>" +
                            "</fetch>";
            //Step 5 - Update The Subrid Context
            gridControl.setFilterXml(fetchXml);
            //Step 6 - Refresh grid to show filtered records only.
            gridControl.refresh();
        }
        //Set grid with query B based fetch XML.
        else {
            gridControl.setVisible(false);
        }
    }
}

Then open form properties and then select the Tab as shown in the screenshot below and attach your JS function with TabStateChange event, save and then publish your changes.

For testing, after opening any task record, just click on the Tab on which you have registered your JS function & it will show the filtered records based on your custom FetchXML query.

That’s it!

Comments

*This post is locked for comments