Wednesday 28 June 2017

bind FetchXML data to subgrid and refresh in Dynamic CRM using JavaScript.


CRM 2015: Applying custom FetchXml to a subgrid using JavaScript
Description:
In this example we explain that how to Bind SubGrid in Dynamic CRM using JavaScript. Or applying custom FetchXML to a subgrid using JavaScript. Or displaying similar data in a subgrid by injecting FetchXML in dynamic CRM using JavaScript. Or bind subgrid dynamically based on value in dynamic CRM.or how to bind FetchXML to subgrid using JavaScript in dynamic crm.or bind FetchXML data to subgrid and refresh the grid in dynamic 365 CRM using JavaScript.

The main point is that when you setup the subgrid with “Record=Only Related Records” in the data source section, the subgrid will append a condition to your FetchXML automatically so that it returns only related record to the specific relationship that you have specified so always try to select “Record=all record types”.

Below is the code that demonstrate how bind subgrid on page load of the opportunity or how to bind Lead subgrid data to the Opportunity subgrid data when Lead is qualified. Or how to copy subgrid data from Lead to Opportunity when lead is qualified.

Javascript Code:

function updateSubGrid()
{
debugger;
var lookupItem= new Object();
    var KiritTeamGrid = window.parent.document.getElementById("KiritTeam"); //replace your subgrid name here
    var originatingleadid= Xrm.Page.getAttribute("originatingleadid");
      if (originatingleadid != null){
         var lookup = Xrm.Page.getAttribute("originatingleadid").getValue();
    if (lookup != null)
   {
      lookupItem.id = lookup[0].id;
    if (KiritTeamGrid == null)
    {
        setTimeout(updateSubGrid, 500); //if the grid hasn’t loaded run this again
        return;
    }

     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>";
     fetchXml += "<entity name='new_kiritteam'>";
     fetchXml += "<attribute name='new_name' />";
     fetchXml += " <attribute name='createdon' />";
     fetchXml += "<order attribute='new_name' descending='false' />";
             fetchXml += "<filter type='and'>";
             fetchXml += "<condition attribute='statecode' value='0' operator='eq'/>";
             fetchXml += "</filter>";
             fetchXml += "<link-entity name='new_new_kiritteam_lead' from='new_kiritteamid' to='new_kiritteamid' visible='false' intersect='true'>";
             fetchXml += "  <link-entity name='lead' from='leadid' to='leadid' alias='ac' >";
             fetchXml += "<filter type='and'>";
             fetchXml += "<condition attribute='leadid' value='"+lookupItem.id+"' uitype='lead' uiname='Alex Wu' operator='eq'/>";
             fetchXml += "</filter>";
     fetchXml += "</link-entity>";
             fetchXml += "</link-entity>";
     fetchXml += "</entity>";
     fetchXml += "</fetch>";
           
    if (KiritTeamGrid.control != null)
    {
        KiritTeamGrid.control.SetParameter("fetchXml", fetchXml);  //set the fetch xml to the sub grid
        KiritTeamGrid.control.refresh();//refresh the sub grid using the new fetch xml
    }
    else
    {
        setTimeout(updateSubGrid, 500); //if the Control is not loaded run this again
    }
}
}


}

1 comments:

  1. Hello!
    Thank you for the code!

    I am trying to populate a subgrid based on the value of look upfield state such that all the accounts of that state appear in the subgrid.

    I am trying to execute the same code but the output is nothing. No error and no output as required. Kindly help!

    ReplyDelete