0
votes

I need some help with my code as the implementation of applying custom FetchXml to a subgrid has appeared to have changed in CRM 2011 to CRM 2013. Please note this is NOT the issue of setParameter vs SetParameter casing as I keep finding all over the net. My issue is how to reference the subgrid and call the SetParameter method at all.

Here is my code:

function uc_addCaseFilterToCaseGrid() {
   var contactGuid = Xrm.Page.data.entity.getId();

  //create a filter xml
   if (contactGuid != null) 
   {
          var filter = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                filter += "<entity name='uc_crecord'>";
                filter += "<attribute name='uc_casemanager' />";
                filter += "<attribute name='uc_cindividual' />";
                filter += "<attribute name='uc_rindividual'/>";
            filter += "<filter type='or'>" +
                       "<filter type='or'>" +
                        "<condition attribute='uc_casemanager' operator='eq' value='" + contactGuid + "'/>" +
                        "<condition attribute='uc_cindividual' operator='eq' value='" + contactGuid + "'/>" +
                        "<condition attribute='uc_rindividual' operator='eq' value='" + contactGuid + "'/>" +
                        "</filter>" +
                     "</filter>" +
                         "</entity>" +
                 "</fetch>";

        //add filter
            //var caseGrid = Xrm.Page.getControl("gPR");
           ***//var caseGrid = Xrm.Page.ui.controls.get("gPR");
           var caseGrid = document.getElementById("gPR").control;***

            if (caseGrid == null) 
            {
               //The caseGrid hasn't loaded, wait 1 second and then try again     
               setTimeout(uc_addCaseFilterToCaseGrid, 3000);
               return;
            }

            caseGrid.control.SetParameter("fetchXML",filter);
            caseGrid.control.Refresh();
    }
}

This code does not work because the bold, italisized line is causing me a problem.

If I use getElementByID (not recommended, and not the approach I'd like to take) the code SetParameter executes as expected.

However, I want to use the following to set the variable:

var caseGrid = Xrm.Page.getControl("gPR");

as I want to ensure future support. But when I call SetParameter I get an error "Object doesn't support property or method 'SetParameter'". What gives?

I've tried the following:

caseGrid.control.SetParameter("fetchXML",filter);

caseGrid.SetParameter("fetchXML",filter);

Xrm.Page.getControl(gPR)._control.get_innerControl()._element.control.SetParameter("fetchXML",filter);

I've tried setParameter and SetParameter and both yield the same result.

I've tested the fetchXml using the XRMToolkit and its fine so this is not the issue (a long shot but it might have been!)

I'm going around the bend trying to figure it out - is it possible that you just can't use SetParameter anymore in CRM 2013 (but you could in 2011)? If so, how else can I supply the fetchXml for a subgrid?

Tested in IE11, CRM 2013 Update Rollup pack 1 + Service Pack 1.

Thanks for your time.

2

2 Answers

4
votes

Feel free to stop trying to go around the bend. Welcome to CRM 2013 and 2015.

The 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, albeit it is not supported and technically could be broken by an update.

Btw, it wasn't actually supported in 2011 - if you look at the SDK there is no documented method to change the FetchXML of a sub-grid. The fact that it worked was (I believe) an artifact of the old Htc grid. With 2013 and 2015 that grid is no longer used so the side-effect no longer works.

Wish this was more of an actual answer instead of just a confirmation of that which you already have discovered.

1
votes

document.getElementById("SubgridName") wont work. Try

var leadwithSameNameGrid = window.parent.document.getElementById("SubgridName"); Now the subgrid wil have the control.Try to have timeout because it wil take sometime to get the control.For getting the control,setting the fetchxml parameter and to refresh use the following code.

function filterSubGrid() {

var leadwithSameNameGrid = window.parent.document.getElementById("SubgridName");

if (leadwithSameNameGrid == null) 
{
    setTimeout(filterSubGrid, 500);
    return;
}

//fetch xml code 

if (leadwithSameNameGrid.control != null) 
{
    leadwithSameNameGrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid   
    leadwithSameNameGrid.control.refresh(); //refresh the sub grid using the new fetch xml
} 
else 
{
    setTimeout(filterSubGrid, 500);
}

}