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
}
In my CRM 2015 installation this code will result in an error
Object doesn’t support property or method ‘SetParameter’
A slightly different code worked in my previous CRM 2013 SP1 installation
document.getElementById(“selectedGrid”).control.SetParameter(“fetchXml”, fetchXmlQuery);
Followed by a …control.Refresh() and preceded with a setTimeout() waiting for the grid control to render everything worked perfectly. Now with CRM 2015 it looks like this code does not render the subgrid control any more. The subgrid will show all records when the page is initially rendered, only when another event triggers the subgrid to be rendered the fetchXml will be processed.
Have you any explanation why this is happening? And should your code not use the control attribute when setting the fetchXml?
LikeLike
Hi Egill,
Thank you for the correction, control was miss on my end. Explecity refreshing the grid after setting the query is similar to what we usied to have for CRM 2011. And the scenario were on-load of page the grid binds all records is something that i’m researching on. As a workaround i have bind a default view to the grid that returns zero records. I have also added the sample code that i used for CRM 2015.
LikeLike
It appears this no longer works after CRM 2015 Update 1.
In the past, one could use Xrm.Page.getControl(“grid”).control.SetParameter(“fetchXML”,fetchxml).
In CRM 2015 the object returned by getControl could not be used to update fetchxml, but one could use document.getElementById(“grid”).control.SetParameter(“fetchXML”, fetchxml) instead.
After Update 1, it appears that DOM scripting is no longer supported – any DOM references in a function result in the function not being parsed properly “function XYX is undefined”.
Does anyone have any idea how to filter a subgrid in JS after CRM 2015 Update 1?
LikeLike
Has anyone found a solution to this problem with CRM 2015 Update 1. I have been looking for a solution for about a week and can’t find anyway to dynamically change the records displayed to a custom set of records.
LikeLike
Issue here is document.getElementById(“selectedGrid”) returns null. I’m trying to figure that out.
LikeLike
I can get this using window.parent.document.getElementById(“selectedGrid”)
But, the problem is that .control is null and so SetParameter is not available. Has anyone found a solution to this? Please post here: http://stackoverflow.com/questions/32564310/dynamics-crm-2015-online-subgrids-control-setparameter-method-is-not-available
LikeLike
Apologies for a strange combination of stupidity from my side:
I received an error using getElementById and found documentation on Update 1 for CRM Online hinting that DOM scripting would no longer work, forgetting/not realising that:
a) I am using CRM On Premise
b) My error on getElementById was due to copying and pasting and therefore having invalid quote characters: “ vs ” !
So getElementById is still working on latest update of CRM On Premise. Has it been confirmed that this no longer works on CRM Online?
LikeLike
I think yes as i’m trying on CRM Online and getting null for getElementById.
LikeLiked by 1 person
use parent.document.getElementByid(your control id).control
it will work
LikeLike
Quote: “use parent.document.getElementByid(your control id).control it will work”
It will not. I get “err: TypeError: parent.document.getElementById(…) is null” when I try.
LikeLike
Here’s the method I’m using. This is for 2015 update 1. We use setTimeout to wait until the grid is instantiated, then push the FetchXML with SetParameter. (mind the quotes if you copy/paste)
filterGrid: function (grid, fetch) {
var objSubGrid = window.parent.document.getElementById(grid);
var isGridLoaded = null;
var self = this;
try {
isGridLoaded = objSubGrid.control.getEntityName();
} catch (err) {
setTimeout(function () { self.filterGrid(grid, guid) }, 500);
return;
}
if (isGridLoaded != null) {
objSubGrid.control.SetParameter(“fetchXml”, fetch);
objSubGrid.control.Refresh();
}
}
LikeLiked by 1 person
I’ve updated the below. This works for 2015 update 1 whether Turbo Forms are on or off.
filterGrid: function (grid, fetch) {
var self = this;
try {
var objSubGrid;
objSubGrid = document.getElementById(grid);
if(!objSubGrid)
objSubGrid = window.parent.document.getElementById(grid);
objSubGrid.control.SetParameter(“fetchXml”, fetch);
objSubGrid.control.Refresh();
} catch (err) {
setTimeout(function () { self.filterGrid(grid, guid) }, 500);
}
}
LikeLiked by 1 person
Dynamics CRM 2015 Online Update 1 you can access the subgrid control using the following code:
var contactsSubgrid = Xrm.Page.getControl(“Contacts”);
LikeLike
contactsSubgrid.control.SetParameter() is not exposed using this method. You can’t use this to override FetchXML for the control.
LikeLike
call this method on Lookup change//
Tested CRM 2016 onpremise version; its working!!
function setCustomers()
{
var id = Xrm.Page.getAttribute(“vrp_customerid”);//Selected Lookup
var name = null;
if(id!=null && id.getValue()!=null)
{
id = Xrm.Page.getAttribute(‘vrp_customerid’).getValue()[0].id;
name = Xrm.Page.getAttribute(‘vrp_customerid’).getValue()[0].name;
var subgrid = window.parent.document.getElementById(“Customers”); // Select Subgrid
if (subgrid == null)
{
setTimeout(‘setCustomers()’, 2000);
return;
}
var fetchXml = “”+
“”+
“”+
“”+
“”+
“”+
“”+
“” +
“” +
“” +
“”;
subgrid.control.SetParameter(“fetchXml”, fetchXml);
subgrid.control.refresh();
}
}
LikeLike