Back to Basics # 71: Filter SubGrid Using a Webresource in Dynamics CRM

Introduction:

There was a requirement to filter records in a subgrid through code without creating a separate View for a record in Dynamics CRM . To achieve this functionality we can leverage SetFilterXML attribute that was present on formcontext object of CRM Form.As an example for a selected account , related contacts for an account [sub grid] will be filtered based on specific email id.

Step 1:

Login to the required environment and navigate to classic account main form of Dynamics CRM Environment and get contact grid Id as shown in the figure.

Step 2:

After Step 1, once you get id of contacts sub grid “Contacts”  and then filter contacts subgrid in Account record by using the following xml code

<filter><condition attribute=’emailaddress1′ operator=’eq’ value=’TestContact2@gmail.com’ /></filter>

As shown in the below figure

 Step 3:

After Step 2, open Webresource and write the below code to filter contacts subgrid

var formContext = executionContext.getFormContext();

    var subgrid=formContext.getControl(“Contacts”);

    subgrid.setFilterXml(“<filter><condition attribute=’emailaddress1′ operator=’eq’ value=’TestContact2@gmail.com’ /></filter>”);

    subgrid.refresh();

As shown in the below figure

Step 4:

After Step 3, final code looks like below

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.AccountForm =

{

    handleOnLoad: function (formContext)

        {

        filterSubGrid(formContext);

        },

    __namespace: true

}

 function  filterSubGrid(executionContext)

{

    var formContext = executionContext.getFormContext();

    var subgrid=formContext.getControl(“Contacts”);

    subgrid.setFilterXml(“<filter><condition attribute=’emailaddress1′ operator=’eq’ value=’TestContact2@gmail.com’ /></filter>”);

    subgrid.refresh();

   }

As shown in the below figure

Step 5:

After Step 4, save and publish webresource and move to account record and observe contacts in the subgrid were filtered as shown in the below figure.

Note:

  1. Make sure to publish all customizations and upload javascript file.
  2. Here I have concentrated on filtering subgrid that’s why hard coded email value.
  3. Make sure to pass execution context as first parameter during registration library in account form load event.

Conclusion: In this way, one can easily filter subgrid using a Webresource in Dynamics CRM.

One thought on “Back to Basics # 71: Filter SubGrid Using a Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s