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
}

Advertisements

21 thoughts on “CRM 2015: Applying custom FetchXml to a subgrid using JavaScript

  1. Egill Pálsson February 5, 2015 / 4:40 am

    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?

    Like

    • Mandar Joshi February 5, 2015 / 10:03 am

      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.

      Like

  2. Doug June 3, 2015 / 2:26 pm

    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?

    Like

    • Rich June 9, 2015 / 2:59 am

      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.

      Like

    • Mandar Joshi June 11, 2015 / 5:22 pm

      Issue here is document.getElementById(“selectedGrid”) returns null. I’m trying to figure that out.

      Like

    • Doug June 12, 2015 / 2:55 pm

      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?

      Like

      • Mandar Joshi June 12, 2015 / 3:38 pm

        I think yes as i’m trying on CRM Online and getting null for getElementById.

        Liked by 1 person

  3. Sandeep V August 24, 2015 / 3:49 pm

    use parent.document.getElementByid(your control id).control

    it will work

    Like

    • Josh August 26, 2015 / 2:39 am

      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.

      Like

      • greensweater February 29, 2016 / 8:38 pm

        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();
        }
        }

        Liked by 1 person

      • greensweater March 18, 2016 / 3:04 am

        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);
        }
        }

        Liked by 1 person

  4. Zsolt Zombik October 6, 2015 / 12:22 pm

    Dynamics CRM 2015 Online Update 1 you can access the subgrid control using the following code:
    var contactsSubgrid = Xrm.Page.getControl(“Contacts”);

    Like

    • greensweater February 29, 2016 / 8:39 pm

      contactsSubgrid.control.SetParameter() is not exposed using this method. You can’t use this to override FetchXML for the control.

      Like

  5. Shital October 30, 2016 / 5:21 pm

    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();
    }
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s