Microsoft Dynamics CRM

{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

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

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