Filtered Subgrid in MSCRM using Javascript
To create a filtered custom subgrid in Dynamics 365 based on a dynamic value, you need to follow these steps:
Create a JavaScript Web Resource:
This script will handle the dynamic filtering logic.
Add the Web Resource to the Form:
Include the JavaScript on the form where the subgrid is located.
Configure the Subgrid to Use the Custom Filter:
Use the JavaScript to dynamically set the subgrid’s fetch XML based on the dynamic value.
Here’s a step-by-step guide to achieve this:
Step 1: Create the JavaScript Web Resource
Go to Settings > Customizations > Customize the System.
In the Solution Explorer, navigate to Web Resources.
Click New to create a new JavaScript web resource.
Name the web resource (e.g., new_dynamicSubgridFilter.js).
Add the following code to the web resource:
If you have two entities without any relation to filter your sub grid then you can use the custom filtered sub grid.
function dynamicallyFilterSubGrid() {
debugger;
var objSubGrid = document.getElementById("Quotes"); //The name of the subgrid
//CRM loads subgrid after form is loaded. so when we are adding script on form load.. need to wait unitil subgrid is loaded.
// thats why adding delay..
if (objSubGrid == null) {// || objSubGrid.readyState != "complete"
setTimeout(dynamicallyFilterSubGrid, 2000);
return;
}
else {
//when subgrid is loaded, get field value on which we are filtering
var oppurtunity = Xrm.Page.getAttribute('new_oppurtunityid').getValue();
// var oppId = "{1DAEF0BB-34E2-E611-8164-00155D55CB07}";
var oppId = "{00000000-0000-0000-0000-000000000000}";// If you are not using related record while creating subgrid
if (oppurtunity != null) {
var FetchOppXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
"<entity name='opportunity'>" +
"<attribute name='name'/>" +
"<attribute name='customerid'/>" +
"<attribute name='opportunityid'/>" +
"<order descending='false' attribute='name'/>" +
"<filter type='and'>" +
"<condition attribute='new_opportunityid' value='" + oppurtunity + "' operator='eq'/>" +
"</filter>" +
"</entity>" +
"</fetch>";
var Results1 = XrmServiceToolkit.Soap.Fetch(FetchOppXml);
if (Results1.length > 0) {
oppId = Results1[0].attributes.opportunityid.value;
}
}
//Create FetchXML for sub grid to filter records based on retrieved values
var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
"<entity name='quote'>" +
"<attribute name='customerid'/>" +
"<attribute name='new_typeofplan'/>" +
"<attribute name='statuscode'/>" +
"<attribute name='quotenumber'/>" +
"<attribute name='opportunityid'/>" +
"<attribute name='createdon'/>" +
"<attribute name='quoteid'/>" +
"<order descending='true' attribute='createdon'/>" +
"<filter type='and'>" +
"<condition attribute='opportunityid' operator='eq' value='" + oppId + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
// Layout of subgrid.
var LayoutXml = "<grid name='resultset' object='8' jump='new_name' select='1' preview='1' icon='1'>" +
" <row name='result' id='new_boat'>" +
"<cell name='quotenumber' width='100' />" +
"<cell name='new_typeofplan' width='200' />" +
"<cell name='statuscode' width='200' />" +
"<cell name='customerid' width='100' />" +
"<cell name='createdon' width='100' />" +
"<cell name='opportunityid' width='100' />" +
"</row>" +
"</grid>";
//apply layout and filtered fetchXML
objSubGrid.control.SetParameter("layoutXml", LayoutXml);
objSubGrid.control.SetParameter("fetchXml", FetchXml);
//Refresh grid to show filtered records only.
objSubGrid.control.Refresh();
}
}
Filtered Custom Subgrid Based on Dynamic Value
https://www.youtube.com/@powerappsninja
POWER APPS PORTALS – JAVASCRIPT TIP #03 – LOAD EDIT MODAL AFTER RECORD CREATION
