Toll Free:

1800 889 7020

How to Filter Sub-Grid Rows in Dynamics 365 CE (CRM) Using Fetch-XML & JavaScript?

INTRODUCTION

Filtering sub-grids dynamically on model-driven forms is a common requirement in Dynamics 365 CRM services implementations—especially when we want to present contextual data based on the record being viewed or edited. Out of the box, sub-grids offer limited flexibility for real-time filtering. However, by leveraging Fetch-XML and JavaScript, we can apply advanced filtering logic that adapts to business rules or user inputs on the fly.

In real-world CRM scenarios—like sales forecasting, opportunity management, or customer service planning—we often deal with interconnected tables and contextual views. Users expect the system to intelligently present related records without navigating away or manually filtering data. This is where programmatic control of sub-grid data becomes a game changer.

This method helps ensure users are presented with the most relevant records based on their actions and selections, making the form behavior smarter and more efficient. Whether you’re customizing forms for Sales, Customer Service, or any other module, this approach gives us the precision and control needed for building intuitive and responsive UIs. By the end, you’ll have a clear understanding of how to implement custom filtering logic for sub-grids in your own Dynamics 365 CE environment.

PRE-REQUISITES:

  1. A Dataverse/Dynamics 365 CE (CRM) environment
  2. System administrator / System Customizer role
  3. (Optional) Access to XRM Toolbox for JavaScript web-resources

STEPS

1. In our Dataverse Search / D365 CE (CRM) environment, there exists a custom table named Account Planning. It has a lookup on it for Account. Despite there not being any relationship between Account Planning and Opportunity tables, we go ahead and place one Sub-grid for Opportunities on Account Planning form (as shown in the screenshot below). We then SAVE the changes.


Dataverse D365 CE (CRM) environment

2. Next, we create one JavaScript web-resource and put the following JS code in it. We all know that there exists an Out-of-the-Box 1: N relationship between Accounts and Opportunities. In simpler words, one Account can have multiple Opportunities. We can see in the highlighted content – we are making a filtration in the Fetch-XML, instructing to pull only those active Opportunities that belong to the selected account.

IS.HPSAAccountPlanning.filterOpportunitiesSubgrid = function (formContext)
{
	var accountControl = formContext.getAttribute("infy_account").getValue();
	if (accountControl)
	{
		var accountId = accountControl[0].id;
		var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
			"<entity name='opportunity'>" +
			"<attribute name='name' />" +
			"<filter type='and'>" +
			"<condition attribute='parentaccountid' operator='eq' uitype='account' value='" + accountId + "' />" +
			"</filter>" +
			"</entity>" +
			"</fetch>";
		// Set the fetchXml to the subgrid
		var gridControl = formContext.getControl("subgrid_opportunities");
		if (gridControl)
		{
			gridControl.setFilterXml(fetchXml);
			gridControl.refresh();
		}
	}
}

3. After Saving & Publishing the above JS web-resource, we add our function to the On Load Handler Property of the Account Planning form. Again, SAVE & PUBLISH.

Handler Properties

UNIT TESTITNG

  • We create a new Account Planning record and select an Account on it.
New Account Planning
  • As we move to the tab where we have placed the Opportunities sub-grid, we verify that only the Opportunities that belong the selected Account get populated in the sub-grid.
Selected Account

Conclusion

In this blog, we explored a practical way to apply custom filters to a sub-grid using Fetch-XML and JavaScript in Dynamics 365 CE. While the platform provides basic sub-grid functionality out of the box, complex business requirements often demand more tailored experience.

Through our example involving the Account Planning and Opportunity tables, we saw how even without a direct relationship, we can still show only the relevant child records—making the user interface more intuitive and aligned with the business context. With just a few lines of JavaScript and a well-formed Fetch-XML, we were able to dynamically filter sub-grid data based on the selected lookup value.

This approach not only enhances user experience but also improves system performance by reducing clutter and ensuring that only meaningful data is displayed. It’s a lightweight yet powerful technique that every Dynamics 365 CRM developer should have in their toolkit.

Read More:

Avatar photo

Yash Shah

Yash Shah is a seasoned technical architect at Aegis Softtech, bringing extensive experience in developing and leading enterprise-level projects. With a broad skill set in areas such as artificial intelligence, machine learning, microservices, and database management, he excels at crafting scalable and innovative solutions. Yash is highly adept at driving project success through technical expertise and strong leadership, ensuring the delivery of high-quality results across a wide range of industries.

Scroll to Top