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
-
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());
}
});
}
-
Call this function before triggering a form reload (e.g., from a ribbon button or subgrid event).
-
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");
}
}
-
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.