Extending Flow with Functions - Part 1
Ever needed to do something in Flow which there isn't already provided in an existing action?
Very often there are packages out there, that we could use to extend our flow with more functionality.
In this article I will show you how to extend flow, using Azure Functions. The technique I'm going to use is:
- Create an Azure Function that take some input, does some processing, and returns a result;
- Test the function to show how it works as an API;
- Write a custom connector, so we can integrate with Flow (and PowerApps);
- Use it in Flow
In the first Part of this series, we will create the Azure Function, and in the second part, we will write the custom connector and use it in Flow.
Give me example of what's missing from flow?
A common senario in our flow is we have data - either from our trigger or another action - such as a SharePoint List Item. We then want to transform this into something else - like the markdown for the body an email, or HTML for web page, or maybe even some json that we going to send into a HTTP action.
So we need something that takes a template, and transform the input to the output - we don't actually have anything in flow.
We could use a compose action, or set variable action, but it is cumbersome, and usually take multiple actions to achieve our output. Even grouping these actions together inside a scope, still just hides the fact that we have to do alot of work inside our flow to transform our data.
But there is very nice little tool - which we use everywhere else - including APIM in Azure - called Liquid Templates!
So in this article, I am going to show, with only a few lines of code, how to get Liquid Template working inside your flow.
Where did the idea come from?
A short distraction - OK - I'll be honest, I was watching a video from Ignite on the difference between Logic Apps and Microsoft Flow, and one of the big selling points of Logic Apps is - Liquid templates.
But wait - I use Liquid templates in my Flow's all the time - it one of the most useful actions I have - so I though I would share with you how I got Liquid templates working in my Flow environment, and show you some examples of why it's so useful.
Lets get started - Our Azure Function
The easiest way to get Liquid Templates is use a package from a package library - such as npm. There are number of liquid template packages, the one I'm going to use is liquid-node.
This technique will work with most npm modules, or a module from other package managers.
Create our Azure Function
Lets start by creating our Azure Function - login to the Azure Portal, and create a Function App
Create a new function, with the "HTTP trigger" template
Install npm packages
Now we need to get the npm library into the function, so launch kudu on your function from the platform features tab
From Kudu - launch a CMD shell
Change directory to your function app, inside the site\wwwroot directory
First - initialise the npm package.json file, with:
npm init
then, install the npm package
npm install liquid-node
Add Javascript code
Return to your function, and now insert the following JavaScript code:
var Liquid = require('liquid-node'); var engine = new Liquid.Engine; module.exports = function (context, req) { var template = req.body.liquidtemplate; var data = JSON.stringify(req.body.data).replace(/\*/g, '¬'); engine.parseAndRender(template, JSON.parse(data)).then( function(result){ context.log(result); context.res = { status: 200, isRaw: true, body: result }; context.done(); } ); };
Here an explaination of the code:
- require('liquid-node') - tells nodejs to use this module;
- new Liquid.Engine - creates an instance of the Liquid engine;
- req.body.liquidtemplate - in the json we receive, the liquid template will be in the liquidtemplateobject;
- req.body.data - in the json we receive, the data we want to transform is inside the data object;
- engine.parseAndRender - this is the actual work - it takes the template and the data, and transforms it to the output;
- context.res - this sends the response back.
Let's test
Lets do a quick test to see if our liquid templates work. So lets take a simple bit of JSON with a person's name it:
{ "name": "Mr PowerApp" }
Lets change this a html heading
<h1>{{name}}</h1>
Click on Test and put in our request body, which is the template in liquidtemplate and our info in data, such as:
{ "data":{ "name": "Mr PowerApp" }, "liquidtemplate":"<h1>{{name}}</h1>" }
Put this into the test area:
Click Run, and you should see the transformed output:
Great - so now you have seen how to select a library from npm, install it on an Azure Function, add the code to execute the package, and test it.
Lets now integrate into our Power Platform - so our Flow and PowerApps can use it. In the next part of this series, we will create a custom connection, and use it in Flow.
*This post is locked for comments