When you think as a code developer and you start with Microsoft Flow there will be a few patterns that you miss. In a first attempt to make every developer welcome, I developed the Try, Catch , Finally pattern in Microsoft Flow. Today I will have a look at being able to create reusable functions (in some languages called methods) in function libraries.
First of all I’m not talking about the functions in Flow that will give you the options to build an expression. In this post I will look at how we can create a flow that can run multiple actions that are put together and form something that you might want to do multiple times in your flow or flows.
Ok, what is a function then …
The Function pattern that I’m describing in this post will assume that:
- We need something that is identified by a function name
- We need something that can take some input
- We need something that can give some output
- We need something that can do something
Why would we need this?
One of the problems I’ve found with the Try Catch template in Microsoft Flow is that you might identify when your flow runs into an issue but it is difficult to find out the details without going to your flow run. If we can run smaller sets of actions which then each have a try catch pattern inside them we might end up with a more workable situation.
Additionally, I would like to avoid many repetitions of the same actions over and over again.
In my example today I’m going to create a PowerApp that will call a flow to make a calculation.
Ok, we can do this in an easier way, but I wanted to create a simple example just to explain the functions library idea.
When I click the button I will run a flow.
The code used to set a variable in PowerApps to whatever the function return is :
Collecting Function details in Flow
In the flow I’m collecting my function details sent my the PowerApp.
Then I take the function details and split them
The split is done with the following expression
Then following the split of my function I created a switch that lists all the functions that I want to support in my function library. The switch is configured to work with the following expression:
Now for each function branch in my switch I created the following steps:
The add function will now take the 2 parameters provided and do whatever it needs to do to create the result that needs to be returned.
In a similar way I created the other functions. Not that I’m only implementing mathematical functions in this post but you could implement approvals or email sending or pretty much anything that takes parameters and generates output using the pattern described in this post.
Now all there is left to do is respond back to my PowerApp and my function library is ready to be used.
Next Steps to create function libraries
In this post you have see how to create function library in Flow. In my next post I will look at how we can now use this pattern. I will then look at how we can call multiple function or how we can make functions call each other. For example, a function could be ask person X for approval and send an email following a certain template to the initiator of the flow. Do ever find yourself create 10 different send an email actions? The next post will solve that.
I will also look at how to implement the try catch pattern within each function so that you can get better error handling implemented in your flows