Understanding Subgrid functions in CRM Online - CloudFronts

Understanding 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.

getTotalRecordCount():

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

Features:
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  

Xrm.Page.getControl("Contacts").addOnLoad(myContactsGridOnloadFunction);  

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

Xrm.Page.getControl("Contacts").refresh() 

getRows().getLength()

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

Features:
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.

Uses:

  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();

Share Story :

Secured By miniOrange