In Dynamics 365 / Power App, we may want to “do something” when a user clicks on a form tab. Let’s look at how we can write a handler to invoke custom code.
In our example, let’s look at the Account form. This form has multiple tabs – Summary, Project Price Lists, General etc. Let’s write code so when a user clicks in and out of the General tab, our code runs:
Edit the form and go to the General tab properties:
We see the tab is actually called Partner_Details. Click OK:
Click to add a new script on the Form Properties:
Click Add:
Create a new JScript, ours called DoAccountStuff.js:
Add the following code. We will call this on the on load of the Account form. Note we are using addTabStateChange to attach our custom function TabClicked to the clicking event:
function OnLoad(executionContext) { formContext = executionContext.getFormContext(); var PartnerDetailsTab = formContext.ui.tabs.get("Partner_Details"); PartnerDetailsTab.addTabStateChange(TabClicked); } function TabClicked() { alert("Tab Clicked"); }
Click to Save the Script, and add it to the OnLoad:
Passing the Context:
Now, on the Account form, click on the General tab. We see our script is invoked:
Note we are passing the executionContext, with the form context, which is useful to do something more meaningful in our script.
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL
Do you know how to do this in the new form editor?
Hi Carl,
thanks for your supports. There is something wrong with this, alert is being triggered for each tab for me not only selected one. Do you have any idea, why it triggers for all tabs?
Seems that this fires when the tab is clicked on and then again when another tab gains focus. Is it possible to only trigger when the specific tab is selected?
Same here. The fact is that the event is triggered on tab state change. This means that one tab is being activated and another is being deactivated (i.e. they are BOTH changing state).
My best guess is that you need to use the above in conjunction with the getDisplayState / getVisible / and getName methods so that you can identify which tab / state is triggering the call.