Be more Productive by using Inline Code in your Azure Logic App
Tags: azure, cloud, cloud5mins, inlinecode
Published: 2019-06-14 10:40:04

---
title: Be more Productive by using Inline Code in your Azure Logic App
published: true
tags: azure,cloud,cloud5mins,inline code
canonical_url: http://www.frankysnotes.com/2019/06/be-more-productive-by-using-inline-code.html
---

In a project using Azure Logic Apps that I am working on, I needed to manipulate strings. I could create APIs or Azure Functions, but the code is very simple and is not using any external libraries. In this post, I will show you how to use the new Inline Code to execute your code snippet directly inside your Logic Apps.

### Quick Context
  
The Logic App will read a file from my OneDrive (it will also work with DropBox, Box, etc.). Here an example of the file:  
  

```
Nice tutorial that explains how to build, using postman, an efficient API.[cloud.azure.postman.tools]
```
  
The goal is to extract tags, contained between the square brackets, from the text.  
  

### Logic App: Get File Content
  
From the Azure Portal, create a new Logic App by clicking the big green "+" button in the top left corner and searching for Logic App.  
  
For this demo, I will use the _Interval_ as a trigger because I will execute the Logic App manually.  
  
The first step will be a **Get File Content** action from the OneDrive connector. Once you authorized Azure to access your OneDrive folder, select the file you want to read. For me, it's `/dev/simpleNote.txt`  
  

### Integration Account
  
To access the **workflowContext** the Azure Logic App required an Integration account. Next step would be to create one. Save the current Logic App, and click on the big "+" button in the top right corner. This time search for integration. Select **Integration Account** , and complete the form to create it.  
  

[![](https://1.bp.blogspot.com/-GWYRjJYFpAk/XQKuEXiwVzI/AAAAAAAA9Eg/qwzrnrrdUP8MydX2oSRWAizZHlaC7AungCLcBGAs/s640/SetIntegrationAccount.png)](https://1.bp.blogspot.com/-GWYRjJYFpAk/XQKuEXiwVzI/AAAAAAAA9Eg/qwzrnrrdUP8MydX2oSRWAizZHlaC7AungCLcBGAs/s1600/SetIntegrationAccount.png)
  
We now need to assign it to our Logic App. From the Logic App blade, in the options list select Workflow Settings. Then select your integration account, and don't forget to save!  

  

### Logic App: Inline Code
  
To add the action at the end of your workflow, click the _New step_ button. Search for **Inline Code** , and select the action **Execute JavaScript Code**.  
  

[![](https://1.bp.blogspot.com/-MZY5Yq_cZZo/XQKuDVCiRKI/AAAAAAAA9EY/LxVwK2SlhVw9dw3uofRePmzcYbIToGgIACEwYBhgL/s640/ExecuteJSCode.png)](https://1.bp.blogspot.com/-MZY5Yq_cZZo/XQKuDVCiRKI/AAAAAAAA9EY/LxVwK2SlhVw9dw3uofRePmzcYbIToGgIACEwYBhgL/s1600/ExecuteJSCode.png)
  
Before copy-pasting the code into the new _Inline Code_ action let's have a quick look.  
  

```javascript
var note = "" + workflowContext.actions.Get_file_content.outputs.body;
var posTag = note.lastIndexOf("[") + 1;
var cleanNote = {};

if(posTag > 0)
{ 
   cleanNote.tags = note.substring(posTag, note.length-1); 
   cleanNote.msg = note.substring(0,posTag-1); 
}
return cleanNote;
```
  
On the first line, we assign a variable _note_ the content of the **Get\_file\_content** outputs. We access it using the workflowContext. This context has access to the trigger and the actions. To find the name of the action you can replace the spaces by the underscore character "\_".  
  

[![](https://1.bp.blogspot.com/-SbmvidS1dFs/XQKuDXOQPCI/AAAAAAAA9Ew/jOdt_uYOnNwGZ3cfLmw7AS8OsL71t2tfACEwYBhgL/s640/CodeView.png)](https://1.bp.blogspot.com/-SbmvidS1dFs/XQKuDXOQPCI/AAAAAAAA9Ew/jOdt_uYOnNwGZ3cfLmw7AS8OsL71t2tfACEwYBhgL/s1600/CodeView.png)
  
You can also switch to Code View, and see the name of all components from the JSON code.  
  

### Logic App: Use Inline Code Result
  
Of course, you can use the output of your Inline Code with other steps. You just need to use the _Result_ from the dynamic content menu.  
  

[![](https://1.bp.blogspot.com/-_--r8MfeMKo/XQKuEZRPSKI/AAAAAAAA9E4/IvughQqHNfMiq0Mki1YhitlGG_81Hqp2gCEwYBhgL/s640/SetVariable.png)](https://1.bp.blogspot.com/-_--r8MfeMKo/XQKuEZRPSKI/AAAAAAAA9E4/IvughQqHNfMiq0Mki1YhitlGG_81Hqp2gCEwYBhgL/s1600/SetVariable.png)
  
If for some reason the dynamic content list doesn't contain your Inline Code, you can always add the code directly `@body('Cleaning_Note')?['body']`.  
  

[![](https://1.bp.blogspot.com/-Ryee22dXo8k/XQKuDZaxfbI/AAAAAAAA9Ew/-kWRujzZQnkvDTD5tN5g3REh6N7hZe42wCEwYBhgL/s640/CodeInCase.png)](https://1.bp.blogspot.com/-Ryee22dXo8k/XQKuDZaxfbI/AAAAAAAA9Ew/-kWRujzZQnkvDTD5tN5g3REh6N7hZe42wCEwYBhgL/s1600/CodeInCase.png)
  
Your Logic App should now look like this:  
  

[![](https://1.bp.blogspot.com/-2sA-vwAQc5I/XQKuFLNBmCI/AAAAAAAA9E8/vv3Ljb-AVXcYWO0puHz11BShbZv4ktiqwCEwYBhgL/s640/final_LogicApp.png)](https://1.bp.blogspot.com/-2sA-vwAQc5I/XQKuFLNBmCI/AAAAAAAA9E8/vv3Ljb-AVXcYWO0puHz11BShbZv4ktiqwCEwYBhgL/s1600/final_LogicApp.png)
  

### Verdict
  
The Inline code is very promising. Right now it's limited to JAvaScript and cannot access variable nor loops. However, for simple code that doesn't require any references, it's easier to maintain and deploy. [You can learn more about what is exactly covered or not here](http://bit.ly/AzLogicAppInline).  
And it works as this result shows.  
  

[![](https://1.bp.blogspot.com/-UGUaF7Ne91c/XQKuFUWpaYI/AAAAAAAA9FA/5TC65hzdgMMv8uaE-fNo8IVeBXPBqES2QCEwYBhgL/s640/RunResults.gif)](https://1.bp.blogspot.com/-UGUaF7Ne91c/XQKuFUWpaYI/AAAAAAAA9FA/5TC65hzdgMMv8uaE-fNo8IVeBXPBqES2QCEwYBhgL/s1600/RunResults.gif)
  

### You prefer watching instead or Reading
  
I also have a video of this post if you prefer.  
 
{% youtube VEQHQlWz-OE %}
  

##### References

- [Microsoft Doc Inline Code](http://bit.ly/AzLogicAppInline)
- [Microsoft Doc Azure Logic Apps](http://bit.ly/AzLogicApp)
- [Microsoft Doc Integration Account](http://bit.ly/IntegrationAccount)
- If you don't have an Azure subscription, [sign up for a free Azure account](https://azure.microsoft.com/free/?WT.mc_id=cloud5mins-youtube-frbouche)