Back to Basics # 64: Get SubGird Record Count with Webresource in Dynamics CRM

Introduction:

During certain scenarios we must get total records count present in a sub grid. To achieve this functionality explicit logic should be written using JavaScript with form controls that are available on the form. Contact form with account sub grid was required and Contact record was 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   below figure.

Step 2:

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

Step 3:

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

Step 4:

After Step 3,  in Webresource javascript write the following code by passing sub grid name by assigning to respective variables using below code

   let accountgridname = “Subgrid_new_1”;

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

And as shown in the below figure.

Step 5:

After Step 4, place the above code in a function and call function from respective event in this case from handleOnLoad function and the final code looks like 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 and publish it on the contact form and open any contact and observe an alert with record count as shown in the below figure

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure to keep code inside settimeout function as to load subgrid DOM takes some time so some delay was required other wise 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.


Discover more from Common Man Tips for Power Platform, Dynamics CRM,Azure

Subscribe to get the latest posts to your email.

One thought on “Back to Basics # 64: Get SubGird Record Count with Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a comment