Hi,
The main idea is to override the "Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml" function to inject your fetch xml that you create dynamically for the sub-grid.
It is not a good idea to override the whole function and it is enough to insert two code lines at the beginning of the function by using "eval" function:
UpdateSetFetchXmlFunc: function (fetchXml) {
var setFetchXmlStr = Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml.toString();
var newFunc = setFetchXmlStr.replace("function(e){", "function(e){if (e.indexOf('ZZZAAA') >= 0) {e = fetchXml;}");
eval("Microsoft.Crm.Client.Core.Storage.DataApi.ListQuery.prototype.set_FetchXml=" + newFunc);
}
where
fetchXml parameter is your fetch xml that you want to use in your sub-grid.
'ZZZAAA' is a specific text to recognize initial fetch xml in your view. For example, "<condition attribute="field_name" operator="like" value="ZZZAAA%" />"
And you can use this function like this:
var grid = Xrm.Page.ui.controls.getByName("sub-grid name");
if (grid) {
grid.addOnLoad(onLoadFunction);
var query = "your query"
UpdateSetFetchXmlFunc(query);
grid.refresh();
}