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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

javascript check if subgrid contains records

(0) ShareShare
ReportReport
Posted on by 600

Hi

I have tried this code from this thread: https://community.dynamics.com/crm/f/117/t/234394. But I cant get the code to work. The script always returns 0, even if the subgrid cointains records.

Anyone have an idea to how a can get a script to show me if a subgrid contains records or not? 

Thanks!

function CheckRows() {
    var grid = Xrm.Page.getControl("children_subgrid");
    if (grid == null) { //make sure the grid has loaded 
        setTimeout(function () { CheckRows(); }, 2000); //if the grid hasn’t loaded run this again when it has 
        return;
    }
    var filteredRecordCount = grid.getGrid().getTotalRecordCount();
    if (filteredRecordCount == 0) {
        grid.setVisible(false);
    }    
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    You might want to increase the timeout to 5000.

    passion4dynamics.com/.../count-total-subgrid-records-in-dynamics-crm-using-javascript   this worked for me fine.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    Since you are using crm online, try to use execution context.

    [View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/clientapi-grid-context]

    [View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/grids/grid/gettotalrecordcount]

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Okay , this will work.

    function CheckRows() {

    debugger;

    try {

    setTimeout(function () {

    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("children_subgrid") != null && Xrm.Page.getControl("children_subgrid") != undefined) {

    var count = Xrm.Page.getControl("children_subgrid").getGrid().getTotalRecordCount();

    if (count==0)

    {

    Xrm.Page.getControl("children_subgrid").setVisible(false);

    }

    alert("Total Records:"+count);

    }

    }, 5000);

    }

    catch (e) {

    Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);

    }

    }

  • martingr Profile Picture
    600 on at

    Thanks!!! it worked.

    I tried adding an else if with alert just for checking. The alert shows a brief time then dissapears. Do you know why the alert message doesnt stay on the page?

    Thanks again

    function CheckRows() {

    debugger;

    try {

    setTimeout(function () {

    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("Forsikringsplaner") != null && Xrm.Page.getControl("Forsikringsplaner") != undefined) {

    var count = Xrm.Page.getControl("Forsikringsplaner").getGrid().getTotalRecordCount();

    if (count==0)

    {

    alert("Total Records:"+count);

    Xrm.Page.getControl("Forsikringsplaner").setVisible(false);

    }

    else if (count !=0)

    {

    alert("Total 3434:"+count);

    }

    }

    }, 5000);

    }

    catch (e) {

    Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);

    }

    }

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    If you are trying to get the count of records in the onload event you can also retrieve subgrid entity record  using WEB API to get rid of settimeout part .

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans