Add New (+) button behavior of sub grid in D365

While working with a sub-grid in D365, a question was thrown regarding behavior of Add record (plus) button. Many of you must have observed that for few sub-grids, click action of Add record button opens a new form to create a new record while for others a new line is added in sub-grid where one can add the record name and associate it. You might have figured the reason for this earlier but it was now that a college helped to figure out why it happens for me.

For those who were still looking for the reason of this behavior like me, this is all related to requirement level of the look-up field on child record. If Look-up is configured as business required then a new form is opened else the grid provides an inline row to associate the record. My assumption for this behavior is that if the look-up is business required, then it is assumed that all the existing records are already associated with some parent record thus user should create a new record. Always correct me if i’m wrong 🙂

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
}