Sunday, August 7, 2011

CRM 2011 - Change Subgrid View Java Script

Use This java script On Form Load...

Change the Fetch XML and set the subgrid ID
function UpdateSubGrid()
{
    var leadGrid = document.getElementById("Contact");//Set Subgrid ID  
  //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding
  if (leadGrid.readyState != "complete")
  {
      //The subgrid hasn't loaded, wait 1 second and then try again
      setTimeout('UpdateLeadSubGrid()', 1000);
      return;
  }


 var fetchXml = "              ";//Set Fetch XML
  //Inject the new fetchXml
  leadGrid.control.setParameter("fetchXml", fetchXml);
  //Force the subgrid to refresh
  leadGrid.control.refresh();


}

6 comments:

  1. Does this work anymore? I have added this code for CRM 2011 on-prem, but it doesnt work for me

    ReplyDelete
  2. ya. This work for me.
    This is my Code.Set Grid Id Correct.

    function UpdateSubGrid() {
    var leadGrid = document.getElementById("Contact");

    //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding
    if (leadGrid.readyState != "complete") {
    //The subgrid hasn't loaded, wait 1 second and then try again
    setTimeout('UpdateLeadSubGrid()', 1000);
    return;
    }

    var accID = Xrm.Page.data.entity.getId();
    // alert(accID);
    var fetchXml = ""
    + " "
    + " "
    + " "
    + " "
    +" "
    +" "
    + " "
    + ""
    +" "
    + " "
    + " "
    +" ";
    // alert(fetchXml);
    //Inject the new fetchXml
    leadGrid.control.setParameter("fetchXml", fetchXml);
    //Force the subgrid to refresh
    leadGrid.control.refresh();
    }

    ReplyDelete
  3. Sorry My Fetch IS

    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"
    + " <entity name='contact'>"
    + " <attribute name='contactid' />"
    + " <attribute name='mship_fullname' />"
    + " <attribute name='fia_isnominatedrepresentative' />"
    +" <link-entity name='fia_membercontact' from='fia_contactid' to='contactid' >"
    +" <filter type='and'>"
    + " <condition attribute='fia_memberid' operator='eq' value='" + accID + "' />"
    + "<condition attribute='statuscode' operator='eq' value='1' uiname='' uitype='' />"
    +" </filter>"
    + " </link-entity>"
    + " <order attribute='fia_isnominatedrepresentative' descending='false' />"
    +" </entity></fetch>";

    ReplyDelete
  4. Hi, I cant get this to work. Can you help? My code is:

    function UpdateSubGrid() {

    var leadGrid = document.getElementById("Test");

    //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding
    if (leadGrid.readyState != "complete") {
    //The subgrid hasn't loaded, wait 1 second and then try again
    setTimeout('UpdateSubGrid()', 1000);
    return;
    }

    //Update the fetchXML that will be used by the grid.
    var fetchXml = ""
    + ""
    + " "
    + ""
    + ""
    + ""
    + " "
    + " "
    + " "
    + " "
    + ""
    + "";
    alert(fetchXml);
    //Inject the new fetchXml
    leadGrid.control.setParameter("fetchXml", fetchXml);
    //Force the subgrid to refresh
    leadGrid.control.refresh();
    }

    ReplyDelete
  5. This doesn't work for Form Type = 1. How can i achieve this??

    ReplyDelete
  6. Does anyone know how to update/load a sub-grid using oData instead of FetchXML?

    ReplyDelete