Dynamics 365 -Custom Filter for Subgrids with setFilterXml

Today, I’ll talk about the simple yet highly functional setFilterXml method. In Dynamics 365, if you want to use custom filters for subgrids, you can modify the existing query or add additional conditions using setFilterXml.

Let’s say you’re on the Account table and have a subgrid called subgrid_contacts. Every subgrid is essentially a GridControl. GridControl (or gridContext) is the instance of the grid or subgrid on a form where you want to execute your script. To learn more about how to access and control the subgrid, I highly recommend checking out GridControl documentation here .

Key Note

The setFilterXml method is unsupported, so you won’t find it in the official documentation. Whether you choose to use it is entirely up to you—maybe it will become supported in the future!

Steps to Implement
  1. Access the Subgrid Control
    First, locate the subgrid control on the form. (formContext and Xrm.Page both work!)
  2. Retrieve the Existing FetchXml
    Use getFetchXml to grab the current query.
  3. Modify the Query (optional)
    If you plan to update the existing query, make your changes here. Otherwise, skip this step if you’re replacing it entirely with a new query.
  4. Apply the Custom Filter Using setFilterXml
    Use the setFilterXml method to apply your custom filter logic.
  5. Refresh the Subgrid
    Finally, refresh the subgrid to see the updated results.

Here’s an example of a custom filter I added:

The red arrow in the query is my custom addition. My test data happens to consist of just numbers, so I added a filter like this. But it doesn’t matter you get the idea😄

Subgrid before I applied the filter

Subgrid after I applied the filter

Leave a comment