🔥 The FREE Azure Developer Guide eBook (Summer 2019 Edition) is now available!.
💡 Learn more : Create your first automated workflow with Azure Logic Apps.
Azure Logic Apps are great. You can use them to create a process of multiple steps. Logic Apps can be triggered on a timer, or by an external event, like when a new email comes in. The process that you create with Logic Apps consists out of calls to connectors and data evaluations by conditions. There are a lot of built-in connectors, like the OneDrive and Office365 connector and you can also use your own API, Function App or Logic App as a connector.
If you want to follow along, you'll need the following:
Let's start by creating the Azure Logic App and connecting the integration account to it. We'll do that in the Azure portal.
(Creating the Azure Logic App in the Azure portal).
In the inline code that we are going to write, we will use values from other tasks in the Logic App. We can do that by calling the workflowContext object in the inline code. To be able to access that, we need to couple an integration account to the Logic App. Let's do that:
(Couple Integration Account to Logic App)
That's it! We can now create the Logic App process.
We will create a simple process that uses inline code. The process will start when I upload a new file in a OneDrive folder and then it will manipulate the text in the file using inline code. Let's set it up.
First, create a new text file and save it on your local machine. With the inline code, we will search through the text in the file and replace the word Portal with portal (with a lower case p). Put the following text in the file:
The Azure Portal is great. Sometimes, Azure Portal is written with a capital P, which is wrong.
(The original text file)
Next, we'll create the Logic App process:
Next, we'll create the trigger for the logic app. This will start the Logic App and run the process. Search for OneDrive and choose the When a file is created trigger.
(When a file is created OneDrive trigger)
You will be asked to sign in to OneDrive and give Logic Apps permissions to your OneDrive. Once you have done that, fill in the fields of the trigger:
Next, we'll create the inline code.
(Inline Code action)
Let's configure the inline code action:
var text = workflowContext.trigger.outputs.body; text = text.replace(/Portal/g, "portal"); return text;
The inline code uses the text from the trigger step and replaces the word Portal (with a capital P), with the word portal (with a lower case p). The /Portal/g syntax means that the code searches for all instances of 'Portal'. The g indicates that it needs to search globally, throughout the text. When you click in the Code textbox, you'll see that you can use all the values from the previous step.
Now, we need to create the final step:
(OneDrive Update File action)
Let's configure the Update File action:
That's it! Now click Save to save the Logic App Process.
Now that the process is complete, we can test the Logic App.
(The result of the Logic App run)
And if you now open the text file in OneDrive, you'll see that the content has changed.
(Updated text file)
Logic Apps are really powerful and easy to use. The ability to use inline code in a Logic App process, creates endless possibilities. With inline code, you can enhance the process in any way you like. You can build custom conditions and data manipulations and whatever else you need. You can learn more by checking out the reference to the inline code action. Go and check it out!