Sub-Grid Dependent On Another Sub-Grid.

Vikrant SarodeDyn365CE4 years ago15 Views

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.

  • Add Department entity Sub-Grid as below, here we will select “only related records”

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.

  • Now setup Sub-Department grid as below, make sure now you need to select “All records Types”.

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:

  1. Automate Microsoft’s Teams Meetings using Custom Connector in Power Automate.
  2. SharePoint Integration with Dynamics 356 using Power Automate.
  3. Dynamics CRM No-Code Low-Code Integrations using Power Automate (MS FLOW)
  4. Programmatically Create the Team’s Meeting.
  5. Invoke Power Automate on Creation of New Users in Azure AD
  6. Automate Microsoft’s Teams Meetings using Custom Connector in Power Automate.
  7. Audit History Using Azure Function

Original Post https://vikrantsdynamicsblogs.wordpress.com/2020/07/09/sub-grid-dependent-on-another-sub-grid/

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...