Search

14 December, 2020

Inject Inline javascript code in your Azure Logic App workflow

JavaScript is web developers all time favourite. Writing small snippets of JS code into your application to implement few cool functionality is very tempting and developers; I know one cannot think of any web application which do not have any JS flavor in some form or the other 😀 

Wouldn't it be great if we can use our JavaScript knowledge and inject a JS snippet into an Azure Logic App workflow and pull a magic out of it. I know you are thinking "Yessss! Why Not", so lets see how it an be done. 

[Please note that is article expects you to have the basic understanding of Azure Logic Apps and Functions. You can check on Microsoft Azure documentation for such] 

Our scenario for this article:

We need an Http endpoint which will accept a temperature and will do the Celsius or Fahrenheit conversion accordingly. If we send the temperature as Celsius then it will give us its Fahrenheit value and vice-versa.

In most cases I have seen developers quickly jumps into writing Azure Function to do this kind of simple stuff but after going through this article I hope you would think twice.

Our JSON payload will be as below which we will send while calling the Logic App endpoint. It's just sending the temperature in Celsius and expecting its respective Fahrenheit value.

{
   "temperature":"40",
   "metric":"C"
}

Now the JavaScript snippet to do the conversion is as below. It's just applying the formula and doing the respective Celsius and Fahrenheit conversion after reading the temperature and metric from the http request. If metric is coming as "C" then the result will be it's respective Fahrenheit and vice-versa.

var temp = workflowContext.trigger.outputs.body.temperature; 
var metric = workflowContext.trigger.outputs.body.metric;
var cf; 
if (metric == "C")
    cf = (temp * 9/5) + 32 + " degree Fahrenheit";
else if(metric == "F")
    cf = (temp - 32) * 5/9 + " degree Celsius";
else
    cf = "Invalid input"
return cf;

That's it! really 😲

Now lets create a Logic App and inject this piece of JavaScript snippet. There is an "Inline Code" Operation with an Action "Execute JavaScript Code (preview)" in Logic App workflow which we will use to achieve our result. Yes it's in preview but works just fine. Don't worry... you can safely use it 👍


Below is the full workflow and by looking at it you can pretty well understand what's it doing. So simple! 


Save your Logic App and copy the Http Post Url to test it. I will use Postman rest client to test the endpoint but you can use any rest client of your choice.

Test 1: Sending 20 degree Celsius and getting it's respective Fahrenheit value


Test 2: Sending 68 degree Fahrenheit and getting it's respective Celsius value


Cool! the outputs are as expected. With few line of JS snippet you are able to pull out the magic of temperature conversion which would otherwise need an Azure Function.

Isn't that great! 

Great Job and Congratulations! for coming this far. Hope this article will help you to further explore more on this feature.

Do share with me about your experience and what you have built upon this foundation. You can take it upto any level and integrate. I would love to hear from you.

No comments:

Post a Comment