I have this code:
function FilterCasesSubgrid() {
//var CasesSubgrid = Xrm.Page.getControl("contact").getGrid();
var CasesSubgrid = window.parent.document.getElementById("contact");
if(CasesSubgrid==null){
setTimeout(function () { FilterCasesSubgrid(); }, 2000); //if the grid hasn’t loaded run this again when it has
return;
}
var fetchXml ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='contact'>"+
"<attribute name='fullname' />"+
"<filter type='and'>"+
"<condition attribute='fullname' operator='eq' value='s%' />"+
"</filter>"+
"</entity>"+
"</fetch>";
//Here i set the fetchxml directly to subgrid
CasesSubgrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid
CasesSubgrid.control.Refresh(); //refresh the sub grid using the new fetch xml
}
ERROR :
TypeError: Cannot read property 'SetParameter' of undefined at
FilterCasesSubgrid
This code isnt supported so you shouldnt expect it to work. Using any function which directly accesses the DOM (i.e; window.parent.document.getElementById
) or uses function not defined within the MSDN SDK is unsupported and should be avoided.
However, given that all you seem to be doing is adding a filter, there are supported methods for doing this by setting an existing FetchXML query:
var myView = {
entityType: 1039, // SavedQuery
id:"{3A282DA1-5D90-E011-95AE-00155D9CFA02}",
name: "My Custom View"
}
//Set the view using ContactsIFollow
Xrm.Page.getControl("Contacts").getViewSelector().setCurrentView(myView);
You'll need to wait for the element AND the control property (CasesSubgrid.control).
This was already answered here
Here's the solution:
- We need to use window.parent.document.getElementById
- Wait for the control to load in the DOM.
So the code would look like this:
function FilterCasesSubgrid()
{
//var CasesSubgrid = Xrm.Page.getControl("contact").getGrid();
var CasesSubgrid = window.parent.document.getElementById("contact");
if(CasesSubgrid==null)
{
setTimeout(function () { FilterCasesSubgrid(); }, 2000); //if the grid hasn’t loaded run this again when it has
return;
}
var fetchXml ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='contact'>"+
"<attribute name='fullname' />"+
"<filter type='and'>"+
"<condition attribute='fullname' operator='eq' value='s%' />"+
"</filter>"+
"</entity>"+
"</fetch>";
//Here i set the fetchxml directly to subgrid
if(CasesSubgrid.control != null)
{
CasesSubgrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid
CasesSubgrid.control.Refresh(); //refresh the sub grid using the new fetch xml
}
else
{
setTimeout(CasesSubgrid, 500);
}
}