Power Apps - Make Custom Functions
Recently I've inherited a bunch of Power Apps which are showing their age. They've had a number of add-ons where other developers have tried to tweak/bolt on new functionality. You could argue that some of them have got to a stage where they could do with being started from the ground up, but then the cost/benefits usually out weigh the advantage of that.
If I could give any piece of advice with Power Apps - heck any form of dev work, don't copy and paste your code. If you are repeating something, then that to me is a waste of time, that's what Functions/Methods are for. In Power Apps the problem is compounded... say the customer wants to change how some validation works on a form component or the app sends a series of emails out with different templates, you could end up hunting through heaps of controls looking for it.
Only this week I came across needing to change some validation, the OnChange event had been modified for every control on the form all with the exact same code, every one I would need to go through and change to the new validation each time repeating my code.
That's a whole lot of changes you'd have to filter through and update. You might miss one, or the app might be so complex you're not even sure if you are updating the right thing. Sometimes when you are creating your apps its good to take a moment and think... I might know how the app works, but if another developer takes over can they unpick this quickly to change something?
This is where Functions obviously come in. My post isn't revolutionary, these suggestions have been made before, but hopefully someone new to Power Apps comes across this and it helps!
Can you really make Functions?
You can't, not at the moment and not in the way we're used to, but there are definitely things that can help. Probably the most "official" way of doing things is Components but they have some drawbacks:
This is where hidden buttons can help, arguably it's not the most elegant solution, but to me it feels like a whole lot better than copying code all over controls.
I place the button usually near the top of the Screen, I've stated to mark them with FCN_ and a description of what they perform. The benefit of this is you can just update the controls which were running the repeated code to "Select(FCN_Name)".
This at least reduces the overhead of changing things later down the road and provides some logic to what's actually the intention of the Function you've created.
The button is only local to the screen you are on, I've read a blog where someone switches to a different screen to run their function and then switches back, I guess that's an option but might seem strange to the end user and affect performance.
Another consideration, if you want to fire one function followed by another, you'll need to add something custom for that. The Select() control is asynchronous so it doesn’t wait for the first to complete, unless you are calling the same button twice, errr I mean Function twice 😁.
Finally, I did wonder if there would be a performance hit of running the code in this way but so far I have not experienced any, so that's a good thing. (I'm planning on looking into running some performance monitoring in the future).