Let’s look at the Contacts subgrid on the Account entity:
Note the Contacts subgrid is called Contacts:
The subgrid is a control, and we can get it using getControl:
We can get the FetchXML of the query to populate the subgrid by using getFetchXML:
This looks like:
Let’s look at how to add an additional filter criteria.
In the XrmToolBox, let’s build a new FetchXML query to filter where the city of a contact is New York:
Now, let’s change the double quotes to single quotes:
And put the query on one line:
Now all we need for setFilterXML is the filter part:
<filter><condition attribute=’address1_city’ operator=’eq’ value=’New York’ /></filter>
Let’s run this in the console, and we will refresh the subgrid once complete. Note we are using Xrm.Page, but the preferred method is to use formContext in its place.
var subgrid = Xrm.Page.getControl(“Contacts”);
subgrid.setFilterXml(“<filter><condition attribute=’address1_city’ operator=’eq’ value=’New York’ /></filter>”);
On running this, we see the subgrid now only contains contacts where the city is New York. Note it is keeping the original filtering criteria as well: