Functions Pattern in Microsoft Flow

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.

Functions

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.

PowerApps example

In my example today I’m going to create a PowerApp that will call a flow to make a calculation.

Test App

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.

Run a flow form PowerApps

The code used to set a variable in PowerApps to whatever the function return is :

Set(FunctionResult,FunctionLibrary.Run(Concatenate(Operation.Selected.Value,":",FirstNumber.Text,":",SecondNumber.Text)))

Collecting Function details in Flow

In the flow I’m collecting my function details sent my the PowerApp.

The trigger

Then I take the function details and split them

Function elements

The split is done with the following expression

Split(outputs('FunctionDetails'),':')

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:

first(outputs('FunctionDetailsElements'))

Switch functions

Now for each function branch in my switch I created the following steps:

A function to add two values

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.

add(int(outputs('FunctionDetailsElements')?[1]),int(outputs('FunctionDetailsElements')?[2]))

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.

Respond To App

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

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading