web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Count the number of Records in a Subgrid

(0) ShareShare
ReportReport
Posted on by

I am hoping someone can shed some light on my problem. I am using the OOTB entity "CASE", I have a customer entity called "SAMPLE". One Case can have many Samples.

On the case form I have a field called "Sample Count". The following code calculates the number of records in the subgrid on the case form and adds the value into the Count Field.

I have this code called "onSave" and "onLoad" of the case form.

However, my issue is the sample count does not seem to calculate onload, It updates instantly when I save the case form. 

In my case this is not sufficient, for example if I create a sample from a case and then save and close the sample...I come back to the case form BUT I need to save before the count is correct.  

function CountNumSamples() {
    if (document.getElementById('Samples')!= null) {
        var count=document.getElementById('Samples').control.get_totalRecordCount();
		var x = parseInt(count);
		Xrm.Page.getAttribute("samplecount").setValue(x);
		Xrm.Page.getAttribute("samplecount").setSubmitMode("always");
		
        }
        else {
            setTimeout("CountNumSamples()", 1000);
        }
    }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Count the number of Records in a Subgrid

    Hello,

    Just curious - why don't you use Rollup field? It will be much more easier to use and this will be no-code solution.

  • Community Member Profile Picture
    on at
    RE: Count the number of Records in a Subgrid

    Do rollup fields exist in CRM 2013?

  • Suggested answer
    saadzag Profile Picture
    1,145 on at
    RE: Count the number of Records in a Subgrid

    Hi,

    You can use an On load event.

    example:

    var myContactsGridOnloadFunction = function () { console.log("Contacts OnLoad occurred") };
    Xrm.Page.getControl("Contacts").addOnLoad(myContactsGridOnloadFunction);

    every time you subgrid refreshes you call the get_totalrecordcount

    Regards

    Saad
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Count the number of Records in a Subgrid

    No, Rollup and Calculated fields were released later. Now I see that you tagged post with CRM 2013. My fault.

    If you want to make your solution solid as a rock you should rework your approach to use Plugins instead of JavaScript.

  • Community Member Profile Picture
    on at
    RE: Count the number of Records in a Subgrid

    Can you help me with incorporating this using my code example and variable names?

    am sure console.log is C# not javascript?

  • Community Member Profile Picture
    on at
    RE: Count the number of Records in a Subgrid

    Unfortunately plugins are not an option here...

    any other ideas..greatly appreciated

  • Suggested answer
    nilesh karale Profile Picture
    132 on at
    RE: Count the number of Records in a Subgrid

    Hi,

    Please use below sample code to get the count of records from subgrid on load

    // Sample Code

    function ContactSubGridCount() {
    setTimeout(function () {

    if (Xrm.Page.getControl("Contacts") != undefined) {
    var contactCount = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();

    Xrm.Page.getAttribute("new_noofcontacts").setValue(contactCount);
    Xrm.Page.getAttribute("new_noofcontacts").setSubmitMode("always");

    };
    }, 2000);
    }

  • Suggested answer
    Rawan Ismail Profile Picture
    on at
    RE: Count the number of Records in a Subgrid

    Worked perfectly on Dynamics 365

  • naZir Profile Picture
    850 on at
    RE: Count the number of Records in a Subgrid

    You can try this code.

    function getTotalGridRecordCount()
    {
    debugger;
    var functionName="onLoad";
    try{
    setTimeout( function(){
    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("accountopportunitiesgrid") != null && Xrm.Page.getControl("accountopportunitiesgrid") != undefined) {
    var count=Xrm.Page.getControl("accountopportunitiesgrid").getGrid().getTotalRecordCount();
    alert(count);}},5000);
    }
    catch(e)
    {
    Xrm.Utility.alertDialog(functionName + "Error: " +e.message || e.description);
    }
    }

  • Ahmad Saud Profile Picture
    790 on at
    RE: Count the number of Records in a Subgrid

    thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans