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:

Components

You can make components on your apps - Matthew Devaney has a great post (you've most likely read):

Components have no idea of what's going on around them, they have inputs and they create outputs. Casting my mind back to my old Software Development modules this isn't a bad thing, all the data is contained as an input to the function - that was for C and C# but for Power Apps things are a bit different, often you are repeating code doing the same thing across a bunch of controls on the current screen, components can't really help you there. You could move the whole thing into a component but for a form control, which is only going to be used once, that feels like a waste of time. Trying to set this up would take longer than piecing together an app.

You can create "Behaviour" custom properties in the Component to then run some steps, an example like before - sending an email with a specific template, however only the controls you add to the component can interact with it e.g. a button in the custom component. I can't add the component to the original screen and use it like ComponentX.SendMailTemplateY().

Buttons

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.

Button Drawbacks

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).

Conclusion

There might not yet be a way to achieve everything you need in the way of Functions with Power Apps but hopefully this post gives you some ideas, even if you don't use one of them, use something! Save yourself some time later down the road when you need to update your copied code. I would also love to hear your ideas and examples which worked for you.

Hope it helps

Comments

Popular posts from this blog

Use Power Automate to create a Document Set – The easy way!

Power Automate - Get SharePoint items using Created By/Modified By

Handling British Summer time when querying SharePoint from Power Automate