{Advanced scripting tip}Traversing and reading column values in Sub Grids in Dynamics CRM 2015 Update 1

Before 2015 Update 1, it was very difficult to traverse through the sub grid elements in Dynamics CRM form.

Now with introduction of support for Grid events it has become easier.

Having said that, there are not enough code samples or blogs on this topic.

In a recent project, this became a necessity and I went through the code to read individual column values, sum them as well.

In this example I am putting a save of the column values using a function and alerting on form save:

Here is the code base:

function onSaveSumNotification()

{

      //Get all Rows of Child Grid

      var allRows = Xrm.Page.getControl(“Child”).getGrid().getRows();

      var allGridRowData = [];

      allRows.forEach(function (row, i)

      {

            allGridRowData.push(row.getData());

      });

      var sum = 0;

      //Read values of all the child grid fields and add them

      for (var i = 0; i < allGridRowData.length; i++)

      {

            sum += parseInt(allGridRowData[i].getEntity().getAttributes().getByName(“dynamics_integer”).getValue());

      }

      //Show notification

      Xrm.Utility.alertDialog(“Sum of integers is: ” + sum.toString());

}

 

And the result:

clip_image002

Note: Due to the asynchronous loading of the sub grids it might not be best suited for on load handler

as the sub grid might not have loaded till the point when the on load event gets hit.

Hope it helps and Happy CRMing!

Advertisements