Tuesday 31 January 2012

Creating embedded grid between parent & child entities in CRM form

Last time, I spent 2 days to figure out how to create an embedded-grid showing "child" entities which are related to a parent entity as the following picture:


As you see, in Campaign Activity entity (which is a parent entity), there are many Email entities (which are related entities). So now, I would like to create a navigation when clicked will show you related entities like that.

The key thing needed to understand is: actually, when you create a relationship bet when 2 entities (such as: Campaign Activity & Email), there is an exsiting "tabset" which stores all related entities created. The navigation link just let us to that tabset only. So the task now is: building url to the tabset.

Here is the javascript how to build the navigation link to get the embedded-grid:
function passURLtoNavigation()
{ //Gets navigation item
   var items = Xrm.Page.ui.navigation.items.get();
   for (var i in items)
    {
       var item = items[i];
      var label = item.getLabel();
      if (label == "Email Created")
          { var navId = item.getId();
            var navItem = document.getElementById(navId);
            if (navItem != null && navItem != undefined)
              { var serverUrl = location.protocol + '//' + location.host + '/' +     Xrm.Page.context.getOrgUniqueName();
          var oId = Xrm.Page.data.entity.getId();
          var oType = Xrm.Page.context.getQueryStringParameters().etc;
          var security = "852023";
          var tabSet = "//here is your relationship name";       
                   //building the URL for the navigation link
                   var navUrl = encodeURI(serverUrl + "/userdefined/areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet);
                   navItem.onclick = function () { loadIsvArea(Mscrm.CrmUri.create(navUrl), false ); };
}
}
}
}

So now, you have the subgrid as shown in the first picture. I hope it will be helpful.

2 comments:

  1. This is a great bit of code and has been working well for me. However somewhere between UR8 and UR13 it stopped working.

    ReplyDelete
  2. Answered my own question, this link helped: http://community.dynamics.com/crm/f/117/t/103089.aspx

    ReplyDelete