Get SubGird Record Count With Webresource In Dynamics CRM

Introduction

During certain scenarios, we must acquire the total records count present in a subgrid. To achieve this functionality, explicit logic should be written using JavaScript, with form controls that are available on the form. A Contact form with an account subgrid is required, and a Contact record is used to explain this functionality.

Step 1

Login to the required environment and select required solution (Contact Customizations Solution in this case), as shown in the figure below.

Get SubGird Record Count with Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the figure below.

Get SubGird Record Count with Webresource in Dynamics CRM

Step 3

After Step 2, open contact form and navigate to account subgrid and get the subgrid name as shown in the figure below.

Get SubGird Record Count with Webresource in Dynamics CRM

Step 4

After Step 3, in Webresource Javascript, write the following code by passing subgrid name by assigning it to respective variables using the code below:

let accountgridname = "Subgrid_new_1";
let count = formContext.getControl(accountgridname).getGrid().getTotalRecordCount();

Step 5

After Step 4, place the above code in a function and call function from respective event - in this case, from the handleOnLoad function. The final code looks like the image below:

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext) {
        console.log('on load - contact form');
        getsubgridrecordcount(executionContext);
    },
    __namespace: true
}

function getsubgridrecordcount(executionContext) {
    setTimeout(function() {
        'use strict';
        let formContext = executionContext.getFormContext();
        if (formContext !== null && formContext != 'undefined') {
            let accountgridname = "Subgrid_new_1";
            let count = formContext.getControl(accountgridname).getGrid().getTotalRecordCount();
            alert(count);
        }
    }, 10000);
}

Step 6

After Step 5, save function and update Webresource with this file. Publish it on the contact form and open any contact to observe an alert with record count as shown in the figure below:

Get SubGird Record Count with Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure to keep the code inside settimeout function, as to load subgrid DOM takes some time, so some delay is required -- otherwise you get 0 records.

Conclusion

In this way, one can easily get records present in subgrid so as to perform related validations on a CRM form with Javascript as webresource.


Similar Articles