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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Model driven app form ...
Power Apps
Unanswered

Model driven app form focus behaviour

(1) ShareShare
ReportReport
Posted on by 20

I have a model driven app form with multiple tabs. There is an onLoad JavaScript that sets visibility of tabs and sections depending on data in the record. One of the tabs has a sub grid. When a record on the sub grid is edited and saved and the parent form is reloaded the focus on the form goes back to the first tab, away from the sub grid tab.

 

On investigation I found that if I turn off the onLoad JS on the main form then this behaviour doesn't occur and the focus remains on the sub grid tab.

 

I stepped through the JS to see if I could find out what was causing the focus to change but the focus only changed when the JS completed.

 

How can I keep the focus on the tab with the sub grid when the sub grid has had a record added / updated?

Categories:
I have the same question (0)
  • Suggested answer
    Jon Unzueta Profile Picture
    1,827 Super User 2025 Season 2 on at

    This is a common issue in model-driven Power Apps where the onLoad JavaScript resets the UI state, including the active tab, after a form reload—especially when triggered by subgrid changes. Since you've confirmed that disabling the onLoad script preserves the tab focus, the script is likely causing the tab to reset.

    ✅ Solution: Preserve and Restore the Active Tab

    You can capture the currently active tab before the form reload and restore it at the end of the onLoad script. Here's how you can do it:


    🛠️ Step-by-Step Fix

    1. Store the active tab name before reload
      Use sessionStorage to persist the active tab across reloads.

    function storeActiveTabName(executionContext) {

        var formContext = executionContext.getFormContext();

        var tabs = formContext.ui.tabs.get();

        tabs.forEach(function(tab) {

            if (tab.getVisible() && tab.getDisplayState() === "expanded") {

                sessionStorage.setItem("activeTabName", tab.getName());

            }

        });

    }

    1. Call this function before triggering a form reload (e.g., from a ribbon button or subgrid event).

    2. Restore the active tab in onLoad

      Add this to the end of your onLoad function:

    function restoreActiveTab(executionContext) {

        var formContext = executionContext.getFormContext();

        var activeTabName = sessionStorage.getItem("activeTabName");

        if (activeTabName) {

            var tab = formContext.ui.tabs.get(activeTabName);

            if (tab && tab.getVisible()) {

                tab.setFocus();

            }

            sessionStorage.removeItem("activeTabName");

        }

    }

    1. Make sure to call restoreActiveTab(executionContext); at the end of your onLoad logic.


    🧠 Additional Tips

    • If you're using a ribbon button or custom command to trigger the subgrid record creation/edit, you can hook into that to store the tab name.
    • If the reload is automatic (e.g., via autosave or subgrid event), you might need to use a formContext.data.addOnSave handler to store the tab name.

     

  • Suggested answer
    Gabriel G. Profile Picture
    831 Super User 2025 Season 2 on at
    Hi,
     
    My suggestion is refactor/remove your Javascript function. You simply seem to have pro-code causing behavior. Your form should not reload everytime you add a record to subgrid.

    I suggest you to try to use in-the-box tools instead of your function. If you can't, try to use Javascript to add benefits to in-the-box tools, but don't try to break the PowerPlatform processes (like save process on subgrid or whatever). Otherwise, you get issue like you have right now which require to make a fix... on a fix...

    I hope it help!
    _____________________________________________________________________________
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard