One of the great things about Dynamics 365 is that it allows you to customize entity forms to display the information most relevant to your business. You will quickly find that providing your users with the ability to see, at a glance, a subset of related records when viewing a lead, contact or account record provides immeasurable value.
In this blog, we will learn how to filter Sub-Grid records based on another Sub-Grid.
Scenarios:
I have two grids on forms, for example, “Department” and “Sub-Department”, our client wanted to see sub-departments filtered by Department. Meaning if user select one row from the “Department” grid they want to see only related records in the “Sub-Department” grid.
Solution:
Please follow the below steps to achieve this functionality:
Step 1: Account having a 1: N relationship with Department and Sub-Department and Department having a 1: N relationship with Sub-Department.
First of all, arrange the Department and Sub-Department Sub-Grid in account form as below.
This department Sub-Grid we have to make it as editable, for that, Navigate to control tab, click on add control and select “Editable Grid” and add it.
Step 2: Now, Create one JavaScript Web Resource and add below JS code in it. Don’t forget to replace Sub-Grid name and your FetchXml.
function subgridOnSelect(executionContext){
var selected = executionContext.getFormContext().data.entity;
var Id = selected.getId();
formContext = window.ALLCONTEXT;
addSubgridEventListener(Id,formContext);
}
function addSubgridEventListener(recordid,formContext){
var gridContext = formContext.getControl("Sub_Department");
//ensure that the subgrid is ready…if not wait and call this function again
if (gridContext == null){
setTimeout(function () { addSubgridEventListener(recordid,formContext); }, 500);
return;
}
//bind the event listener when the subgrid is ready
var fetchxml="<fetch version='1.0' output-format="xml-platform" mapping='logical' distinct="false">"+
" <entity name="new_subdepartment">"+
" <attribute name="new_subdepartmentid" />"+
" <attribute name="new_name" />"+
" <attribute name="createdon" />"+
" <attribute name="new_estrevenue" />"+
" <attribute name="new_department" />"+
" <order attribute="new_name" descending='false' />"+
" <filter type="and">"+
" <condition attribute="new_department" operator="eq" uiname="Department 1" uitype="new_department" value=""+recordid+"" />"+
" </filter>"+
" </entity>"+
"</fetch>";
gridContext.setFilterXml(fetchxml);
gridContext.refresh();
}
var ALLCONTEXT;
function Onload(executionContext) {
ALLCONTEXT =executionContext.getFormContext();
}
Step 3: Once web resource is created, Then again navigate to Account form, open Department Sub-Grid, and click on the Event tab.
Here we need to add our newly created Web resource and call “subgridOnSelect” function onRecordSelect event.
While adding JS in Sub Grid, don’t forget to click on the “Pass execution context as the first parameter”.
Step 4: Now one last thing, we need to call “onload” function on account form load event.
Click on form property select our JS and add Onload function as below, again don’t forget to pass execution context.
Now it’s all set, just navigate to an account record and select department record in Sub-Grid, it will auto-filter the Sub-Department grid view records and it will display only records related to the selected Department.
Check out my recent blog post:
Original Post https://vikrantsdynamicsblogs.wordpress.com/2020/07/09/sub-grid-dependent-on-another-sub-grid/