Reuse code in Power Apps – across screens!

What’s this about?

If you’ve ever developed a moderately complex PowerApp, you’ll know the pain: Reusing code is not something Power Apps was built for…
There is a widely known way to do it inside a single screen that I’m going to go over quickly for the sake of completeness.
But what I’m really stoked about showing you is a way to do it across your entire app, no matter what screen you are currently on.

Let’s get to it!

Reuse code inside a screen

This one is pretty straight forward: You put your code in a hidden button and trigger it from anywhere on the screen you need it. Here’s a play by play:

Insert the button

Insert a button anywhere on the screen and give it a speaking name like “Code_DoSomething”.
Set the following properties:

Visible: false
OnSelect: [Your code here]

Call the code

You can now call the code by executing a Select() on the button anywhere on the screen like this: “Select(Code_DoSomething)”
Neat. Now if you need to change the code, you just have to go into the OnSelect property of the button and change it there.

The catch

This only works inside the same screen. You can’t use Select() to interact with controls on a different screen.

Reuse code across screens

Since Select() is not an option across screens and it’s basically the only function of a button, we’ll have to look into a different control. We can interact with a toggle across screens by setting it’s state through a global variable.

Insert a screen

This step is optional. I just like how clean this makes things.

Insert an empty screen and put all your code-reuse-toggles on there. Nobody is ever going to see this screen.
This way you have a single place to go to for all code changes and make management easier for yourself.

Insert a toggle

Insert a toggle anywhere in your app and give it a speaking name if you like, in this scenario it doesn’t really matter, since you won’t be calling the toggle directly. I like to call mine “Toggle Code_DoSomething”.
Set the following properties:

Default: Var_DoSomething
OnCheck: [Your code here]

Now this needs more explanation:
We’re setting the default state of the toggle to a global variable. When your app initializes, this variable will be empty and therefor handled as “false”. As soon as we set that global variable to “true”, the toggle will switch to a checked state and execute whatever it is you put in the OnCheck property.

You don’t need to worry about resetting the state of the toggle. Setting the state variable to “true” will execute the code every time, no matter what the current state of the toggle is.

Call the code

Use “Set()” to set the global variable to “true” from anywhere like this: “Set(Var_DoSomething, true)”. This will trigger your code from anywhere in the app.

Should I still use the button solution if I’m staying inside a single screen?

Honestly? Why would you?
Using the toggle gives you room for future changes to the overall layout of your app.
I can see no reason not to do it that way consistently from now on.

Why is this neat?

A button has no state. Compare it to a physical button:
If you are in a different room, you cannot press it. It’s un-pressed and will remain so until you physically walk up to it and press it.

If you look at a physical switch (representing the toggle), you’ll notice that it has a state and it’s independent from the user. The switch will be either on or off, you being in the room has no impact on that state.

Since the state of the toggle is defined through a variable that is empty on initialization, we sort of have Schrödinger’s toggle on our hands. Good thing empty variables default to false in Power Apps or we’d have to take quantum mechanics into consideration…

Source

Credit where credit is due: I was digging around in the Power Apps Forums on a completely unrelated topic, when I stumbled upon this.
Sancho (iAm_ManCat) posted this as an answer, thanks for bringing this to my attention!

Leave a Reply

Your email address will not be published. Required fields are marked *