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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to add event handler to subgrid of a form?

(0) ShareShare
ReportReport
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.  

  • Verified answer
    Community Member Profile Picture
    on at

    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)

  • Suggested answer
    bp3378 Profile Picture
    190 on at

    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!

  • Community Member Profile Picture
    on at

    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!:)

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 38 Super User 2026 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 32 Super User 2026 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 30 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans