Hi Mohsin,
For D365 CRM V.9 I used this code below, Assuming you already set up the subgrid in the form as "All related records":
function FetchViaName(executionContext) {
var formContext = executionContext.getFormContext();
// Credit_Performance : is name of subgrid given on Form.
// You need the code below because using SetParameter in formContext.getControl("Credit_Performance") will not work.
var objSubGrid = window.parent.document.getElementById("Credit_Performance");
var conSubGrid = formContext.getControl("Credit_Performance");
var subRowCount = conSubGrid.getGrid().getTotalRecordCount(); // Making sure that you the grid is loaded
var formContext = executionContext.getFormContext();
var globalContext = Xrm.Utility.getGlobalContext();
var version = globalContext.getVersion();
// You need the timeout for at least the form is properly loaded.
if (subRowCount == 0) {
setTimeout(FetchViaName(executionContext), 5000);
} else {
// When subgrid is loaded, get the other relevant value.
var curId = formContext.data.entity.getId();
var curName = formContext.getAttribute("dxcts_name").getValue();
var ratingName = formContext.getAttribute("dxcts_searchratingname").getValue();
var workAccount = formContext.getAttribute("dxcts_workaccount").getValue();
//Create FetchXML for sub grid to filter records
var FetchXml = '<fetch version="1.0" output-format="xml - platform" mapping="logical" distinct="false"> '
+ '<entity name = "dxcts_creditperformance" > '
+ '<attribute name="dxcts_creditperformanceid" /> '
+ '<attribute name="dxcts_name" /> '
+ '<attribute name="dxcts_work" /> '
+ '<attribute name="dxcts_ratings" /> '
+ '<attribute name="dxcts_comments" /> '
+ '<attribute name="dxcts_sugestions" /> '
+ ' <attribute name="createdon" /> '
+ ' <order attribute="dxcts_name" descending="false" /> '
+ ' <filter type="and"> '
+ ' <condition attribute="dxcts_work" operator="eq" uiname="' + curName + '" uitype="dxcts_work" value="' + curId + '" /> '
+ ' <condition attribute="dxcts_accountscreditperformanceid" operator="eq" uiname="' + workAccount[0].name + '" uitype="account" value="' + workAccount[0].id + '" /> '
+ ' <condition attribute="statuscode" operator="eq" value="1" /> '
+ ' <condition attribute="dxcts_name" operator="like" value="%' + ratingName + '%" />'
+ ' </filter> '
+ ' </entity > '
+ ' </fetch > ';
// Layout of subgrid. It's up to you if your going to use this code, it's just creating/identifying a new layout.
//var LayoutXml = "<grid name='resultset' object='8' jump='new_name' select='1' preview='1' icon='1'>" +
// " <row name='result' id='new_boat'>" +
// "<cell name='dxcts_name' width='100' />" +
// "<cell name='dxcts_work' width='200' />" +
// "<cell name='dxcts_ratings' width='100' />" +
// "<cell name='dxcts_comments' width='200' />" +
// "<cell name='dxcts_sugestions' width='200' />" +
// "</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();
}
}
Happy Coding! =D