CRM 2015: Applying custom FetchXml to a subgrid using JavaScript

The implementation of applying custom FetchXml to a subgrid has appeared to have changed from CRM 2011 to CRM 2015. The change is with respect to GridControl.SetParameter() but I’m NOT referring to the issue of setParameter vs SetParameter which all know.
If I use Xrm.Page.getControl(“grid”).control.SetParameter(“fetchXML”,fetchxml) I get an error “Object doesn’t support property or method ‘SetParameter’“.
Trying setParameter for SetParameter yield the same result.

The issue here is object returned by Xrm.Page.getControl does not support setting the FetchXML of a sub-grid. Your use of getElementById is the only way to accomplish this goal, though it is not supported and technically could be broken by an update.

So the working version for CRM 2015 is as below:

document.getElementById(“grid”).control.SetParameter(“fetchXML”, fetchxml);

Also when you set up the subgrid with “Records = Only Related Records” in the Data Source section, the subgrid will append a condition to your fetchxml so that it returns only records related to the specific relationship that you specified. So always select the Records as all record types.

One more change with FetchXML is that previously if we want to get all records for a condition, we used to add “*” as a value for that condition. This has changed now, the “*” is now automatically converted to “%%%” while generating FETCHXML.

Sample code:

//Filter connection subgrid
function FilterConnectionSubgrid()
{

var ConnectionSubgrid =document.getElementById(“Contacts”);
if(ConnectionSubgrid==null)
{
setTimeout(function () { FilterConnectionSubgrid(); }, 2000); //if the grid hasn’t loaded run this again
return;
}
var fetchXml = “<your fetchxmlquery here>”

//Set the fetchxml directly to subgrid

ConnectionSubgrid.control.SetParameter(“fetchXml”, fetchXml); //set the fetch xml to the sub grid

ConnectionSubgrid.control.Refresh(); //refresh the sub grid using the new fetch xml
}

Advertisements