Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Passion Dynamics / Count Total Subgrid Records...

Count Total Subgrid Records in Dynamics CRM using JavaScript

Rawish Kumar Profile Picture Rawish Kumar 13,752

In one of my previous blog I explained,how a rollup field can be used to count child records : Count the number of related child records using a Rollup field

However, Rollup field is calculated with an Async System Job, therefore, if you need something to be triggered on every form load; you will have to write a JS.

Here is a quick code to count the number of related sub-grid record on a form onload. You can put an alert of the count or add that number to a field.

function getTotalGridRecordCount() {
 debugger;
 try {

setTimeout(function () {
 if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contactopportunitiesgrid") != null && Xrm.Page.getControl("contactopportunitiesgrid") != undefined) {
 var count = Xrm.Page.getControl("contactopportunitiesgrid").getGrid().getTotalRecordCount();
 alert("Total Opportunities:"+count);

}
 }, 5000);
 }
 catch (e) {
 Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);
 }
}

Add script to the form and call this function on Onload event. Open the form to see it in action:

1

 

Hope this helps! cheers!

Comments

*This post is locked for comments

  • Rawish Kumar Profile Picture Rawish Kumar 13,752
    Posted at

    Hi Martin,

    This is based on subgrid's control. if you need to count the number of notes ( annotations) you can simply use a simple web api call or use fetch xml to get the count.

    here is the code i have generated using rest builder. The count property gives the record count.

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/annotations?$filter=_objectid_value eq 49A0E5B9-88DF-E311-B8E5-6C3BE5A8B200&$count=true", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               var recordCount = results["@odata.count"];

               alert(recordCount);

               for (var i = 0; i

  • martingr Profile Picture martingr 600
    Posted at

    Hi. Thanks for this post. Do you know if it would be possible to count the total of notes in the social pane? Thanks :-)