Filtering in Sub-grids CRM 2015 | CRM online

Filter Sub-grid in CRM 2015

Contextual filtering in sub grids is a very common requirement, which is not supported out of the box in CRM. People use unsupported JavaScript which works till January update 1 and for spring release it stopped working. But if we understand why it will not work in spring release, because of new form structure and change DOM, document.getElementById stopped working.

JavaScript method which will work in January Update and Spring release 

function  getElement(elementName) {

    if  (document.getElementById(elementName) == null || document.getElementById(elementName) == 'undefined') {

        return  window.parent.document.getElementById(elementName);

    }

    else {

        return document.getElementById(elementName);

    }

}

 

The above function will return the object in both January Update 1 and Spring release. Now any sub grid can be filtered in January Update 1 and spring release both. Here is the method:

 

function  LoadGridForRollupView(subGridName, filterFetchXml) {

    var subGrid =  getElement(subGridName);

    //If this method is called from the form OnLoad, make  sure that the grid is loaded before proceeding  

    if (subGrid == null ||  subGrid.control == null || subGrid.control == 'undefined') {

        //The subgrid hasn't loaded, wait 100 mili second and  then try again

         setTimeout('LoadGridForRollupView(\'' + subGridName + '\',\'' + filterFetchXml + '\')', 100);

        return;

    }

 

    //Update the fetchXML that will be used by the grid.

    var FetchXml = filterFetchXml;

    //Inject the new fetchXml

     subGrid.control.SetParameter("fetchXml", FetchXml);

     subGrid.control.SetParameter("effectiveFetchXml", FetchXml);

     subGrid.control.SetParameter("fetchXmlForFilters", FetchXml);

 

    //Force the subgrid to refresh

     subGrid.control.refresh();

    setTimeout('RefreshSubGrid(\'' +  subGridName + '\')', 500);

}

 

 

But again this method is unsupported and it might stop working in subsequent releases of dynamics CRM online. This has been tested on Chrome and IE for CRM online for January Update 1 and spring release both.

 

Happy programming in Dynamics CRM online