Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to add event handler to subgrid of a form?

Posted on by 190

I have a form which includes a subgrid.  I want a field in the main form to show only if there are related items in the subgrid for that particular record.  But when there are no more records (if removed manually) from the subgrid, I want to hide that same field again.  

I know how to apply javascript to event handler on the main form, but i don't know how to add it to the subgrid.  I do not see a event tab in the subgrid properties.  

As of now my scripts can only be based on main form such as onload, onsave, or on change of fields on the main form.  So if I delete a record on the subgrid, i dont want user to need to load, save the main form for the script to run.  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add event handler to subgrid of a form?

    Hi bp3378, 

    I am glad to help you, if my answer is helpful, could you click 'Yes' under the title to close the current case.

    pastedimage1655696821745v1.png

    Thanks in advance!:)

  • Suggested answer
    bp3378 Profile Picture
    bp3378 190 on at
    RE: How to add event handler to subgrid of a form?

    Thank you Leah.  The onload subgrid worked for me.  I didnt get the gettotalrecordcount to work.  It only worked most of the time except when I try to delete the last record.  It seems that the after deleting, the gettotalrecordcount execute too quickly so it still reads that there is one record when there is really no more.  I tried to add a 3 sec delay but i really didnt like the delay.  So what I did is connected to the child table with  XMLHttpRequest() and selected the foreign guid that looks up to the parent record.  If there are no foreign guid for that record then that means there is no records in the subgrid, then i can do what i need to do.  

    Thanks for your  help!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add event handler to subgrid of a form?

    Hi bp3378,

    Maybe you can try to use Subgrid OnLoad event: This event occurs every time the subgrid refreshes. 

    Subgrid OnLoad event (Client API reference) in model-driven apps - Power Apps | Microsoft Docs 

    Register a Function OnLoad of a SubGrid in Dynamics 365 Power Apps - Carl de Souza

    And you can use getTotalRecordCount function, it returns the total number of records that match the filter criteria of the view, not limited by the number visible in a single page.

    getTotalRecordCount (Client API reference) in model-driven apps - Power Apps | Microsoft Docs

    This blog is similar to your needs and may provide you with some inspiration:

    Working with the grid onLoad event | It Ain't Boring (itaintboring.com)

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,316 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans