30 Dec’16

Dynamics 365, EnterpriseUnderstanding Subgrid functions in CRM Online

Using the new subgrid functions, there are 2 ways to get count of the subgrid data. Based on requirement, we have to use one or the other way for achieving the requirement.


It determines the total no. of records that match the filter criteria of the view.

1. The count is NOT limited by the current page size. You will get the total size across all pages.
2. You may not also get the latest count of the records using this function if new records are added on the subgrid.

How to get around #2 above to get correct count:

  1. The idea is to make the grid refresh before executing the “getTotalRecordCount” function, which will enable us to get the correct count.
  2. We also have to add an onload event handler for the grid control. Whenever grid is refreshed through code or manually, the onload function will trigger.
  3. Note that we cannot do this without onload event. If we try to execute “getTotalRecordCount” just after the refresh line without onload event handler, you will get an error as the grid refresh happens asynchronously and it may not be ready.

Sample Code

 var myContactsGridOnloadFunction = function () {  
     console.log("Contacts Subgrid OnLoad occurred"); 
     var count = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();   

/// bind an onload event to the grid control  


/// refresh the grid. This will trigger onload event.  



getRows() will return collection of all gridrows in the grid.

1. It will only return the rows currently visible on the grid.
2. getRows().getLength() will return the count of rows currently visible
3. Unlike getTotalRecordCount, we need not refresh the grid to get the correct row length using this function.


  1. In cases, where we want to know if the grid is empty we can use “getRows().getLength()” instead of getTotalRecordCount to know if the count is 0 or Not.

Sample code:

var rows = Xrm.Page.getControl(opptyProductSubGridName).getGrid().getRows(); 
var rowLength = rows.getLength();
Written by

Team Member


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Reading more then 10K records in D3FOE OData API

    11 July’ 2018

    Introduction: We all know Dynamics 365 Finance and Operations has limitation of 10K records to be fetched at a time usi...

    Read more
  • Paging in D365 Customer Engagement v9.0

    10 July’ 2018

    Introduction: The Xrm.retrieveMultipleRecords method is used to retrieve a collection of records in Dynamics 365 Custom...

    Read more
  • Set up Dynamics 365 connection in Microsoft Social Engagement

    10 July’ 2018

    Introduction: This blog explains how to Set up Dynamics 365 connection in Microsoft Social Engagement. Steps to be follo...

    Read more
  • Voice of the Customer failed to install

    10 July’ 2018

    Introduction: Many people face issues in installing Voice of Customer solution on v9 environment and trying repeatedly ...

    Read more
  • Scribe Insight AX as a Web Service Find Block issue

    10 July’ 2018

    Introduction: If we need to look up for any value from AX then we do it by using a Find Block in Scribe Insight Eg: Basi...

    Read more