AZURE LOGIC APPS – HOW TO USE INLINE JAVASCRIPT CODE SNIPPETS

  • In this demo, we will be creating an Azure Logic App that will use inline JavaScript.
  • In order to reference values from previous Logic App steps in JavaScript, we will be creating an integration account.
  • Then, we will couple that integration account with our Logic App.
  • The Integration Account is part of the Logic Apps Enterprise Integration Pack (EIP) and is a secure, manageable and scalable container for the integration artifacts that you create.
  • You can store maps, schemas, partners, agreements and certificates in your Integration Account once and reference them across all your logic apps making the creation of B2B processes with logic apps quick and easy.

Go to Azure Portal. Click Create a resource and search for Integration Account. Once the blade opens, click create.

Give a name to your integration account. Create a new resource group for it. Select the Standard pricing tier. Choose the location nearest to you. Keep log analytics off and click create.

Next, click Create a resource. Go to Web category and choose logic app.

Give name to your Logic app. Choose the same resource group that you created earlier. Choose the same location as well. Keep log analytics off and click create.

Once the Logic app gets created, we need to couple it with the integration account. To do that, go to Workflow settings.

Select your integration account and click Save.

Next, we will design the logic app. Go to the logic app designer and choose a blank logic app. We are going to start from scratch.

  • The process that we are going to build will get a text file from OneDrive and replace text in it.

So for that, we will use the OneDrive trigger.

And we will want to trigger when a file is created.

Sign in to create a connection to OneDrive.

Once you establish the connection, select the folder from your drive. Keep the interval time as 10 seconds. Click New Step.

Next, we are going to use an inline code. So, choose that action.

Choose the ‘Execute JavaScript Code’.

Replace the default JavaScript code with this code. This code first takes the file content from the previous step. We needed the integration to access the object ‘workflowContext’. Then we take the text and replace the word ‘Portal’ with ‘portal’. The g means that it replaces it globally. Then we finally return the results.

var text = workflowContext.trigger.outputs.body;

text = text.replace(/Portal/g, "portal");

return text;

Click Next step. Choose OneDrive again. Choose the Update file trigger.

Choose the File identifier in the file field.

Sometimes choosing the file might result into an error. Since we don’t want that error, rename the file.

Rename the file to ‘Replace Text’.

Now we can reference the result by typing body of ‘Replace_Text’ action. Then click Save.

Create this sample text file on your machine with this text.

Upload it to OneDrive in the InlineLogicApp folder.

Run the Logic app.

Now open the file in OneDrive. You will see the replaced text in the text file.

  • With inline code, you can enhance the processes in any way you like.
  • You can build custom conditions and data manipulations, etc.