While often times the values of a sub-grid can be defined only through the grid settings, you will find that there are situations where you want the grid to display a set of custom results. You can control the results selection/filtering using FetchXML, and here’s how to do it:
First off, I have a function that triggers on page load. It looks like this:
function PopulateRecentCaseSubGrid() {
var ruleConditionID = Xrm.Page.data.entity.getId(); //Get Rule ID
var ruleConditionName = Xrm.Page.getAttribute("new_name").getValue();
if (ruleConditionID != null) {
var guid = ruleConditionID.substring(1, ruleConditionID.length – 1);
var fetchXml = "<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>" +
"<entity name=’incident’>" +
"<attribute name=’title’ />" +
"<attribute name=’ticketnumber’ />" +
"<attribute name=’casetypecode’ />" +
"<attribute name=’statecode’ />" +
"<attribute name=’new_project’ />" +
"<attribute name=’ownerid’ />" +
"<attribute name=’incidentid’ />" +
"<order attribute=’title’ descending=’false’ />" +
"<filter type=’and’>" +
"<filter type=’or’>" +
"<condition attribute=’new_rule_condition1′ operator=’eq’ uiname=’" + ruleConditionName + "’ uitype=’new_rulecondition’ value=’" + guid + "’ />" +
"<condition attribute=’new_rule_condition2′ operator=’eq’ uiname=’" + ruleConditionName + "’ uitype=’new_rulecondition’ value=’" + guid + "’ />" +
"<condition attribute=’new_rule_condition3′ operator=’eq’ uiname=’" + ruleConditionName + "’ uitype=’new_rulecondition’ value=’" + guid + "’ />" +
"<condition attribute=’new_rule_condition4′ operator=’eq’ uiname=’" + ruleConditionName + "’ uitype=’new_rulecondition’ value=’" + guid + "’ />" +
"<condition attribute=’new_rule_condition5′ operator=’eq’ uiname=’" + ruleConditionName + "’ uitype=’new_rulecondition’ value=’" + guid + "’ />" +
"</filter>" +
"</filter>" +
"</entity>" +
"</fetch>";
setTimeout(function () {
PopulateGridFetchXML(‘Compliance_Cases’, fetchXml);
}, 5000)
}
}
This defines the FetchXML with all the required filters, and calls another function called PopulateGridFetchXML.
function PopulateGridFetchXML(subGridName, fetchXML) {
try {
var subGrid = document.getElementById(subGridName);
subGrid.control.SetParameter("fetchXml", fetchXML);
subGrid.control.refresh();
} catch (err) {
alert(err.message);
}
}
Our PopulateGridFetchXML function takes in the name of the sub-grid we want to refresh, along with the fetch query. It simply sets the FetchXML parameter to the grid, and calls a refresh on it.
You can build your FetchXML using advanced find or manually.
Enjoy!
Hi Nico, great post! Is there a reason you use document.getElementById() instead of the supported Xrm.Page.ui.controls.get()? Interfacing with the DOM might make it break :-)
LikeLike
No reason other that it’s an older script. It should work the same, and you are correct, new scripts should use Xrm.Page
LikeLike