Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Check if a subgrid is empty and then display it or not

Posted on by Microsoft Employee

Hi,

I would like to check if my subgrid contains any records and then if it's empty I want it to be invisible with a script. I'm also asking myself where can I see the exact ID of my subgrid ? 

Regards,

Antoine

*This post is locked for comments

  • Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Check if a subgrid is empty and then display it or not

    If you got the answer, please close the thread as verified.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Check if a subgrid is empty and then display it or not

    THANKS YOU SO MUCH IT WORK PERFECTLY !!

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Check if a subgrid is empty and then display it or not

    Hi Antoine,

    Here the subgrid name is children_subgrid.

    Please set a timeout function to ensure that the subgrid is loaded.

    Change the code as below.

    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);
        }    
    }
    
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Check if a subgrid is empty and then display it or not

    2477.Sans-titre.png

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Check if a subgrid is empty and then display it or not

    Antoine,

    What is the name of the subgrid in the form? Can you share a screenshot of the Set Properties window.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Check if a subgrid is empty and then display it or not

    The code seems good but i get an error and i don't know why, when i subsitute "subGridgridName" by my own SubGrid name, do i have to put the publisher prefix ? (xx_subGridgridName or just subGridgridName ?), does this code work for you ?

    Thanks you anyway

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Check if a subgrid is empty and then display it or not

    Hi Antoine, 

    Try using the following code.

    function CheckRows() {
        var grid = Xrm.Page.getControl("subGridgridName");
        var filteredRecordCount = grid.getGrid().getTotalRecordCount();
        if (filteredRecordCount == 0) {
            grid.setVisible(false);
        }    
    }

    For getting the subgrid name, go to Form Editor. Select the subgrid and click Change Properties in the command bar as shown in the screenshot below.

    65576.2.PNG

    In the Set Properties window that appears, we can get the name of the subgrid as highlighted below.

    65576.2.PNG

    Hope this helps.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans