In my previous post, we've walked through a Logic App workflow to get the latest item in an array, by combining the Select
action and the Filter
action. In fact, although this approach is practical, it is only applicable for a few specific use cases, and a workaround, which is a bit tricky to apply in general. But, this preview feature, Inline JavaScript Code action, can be handy for array sort. In this post, I'm going to discuss how to use the Inline JavaScript Code action to sort array items and take the latest one in the Logic App workflow.
Integration Account
In order to use this Inline JavaScript Code action, we have to provision an Integration Account instance. There are three pricing tiers of Integration Account – Free, Basic and Standard. For our practice, the free one is more than enough.
Once the Integration Account instance is provisioned, connect it with the existing Logic App instance to use the action.
JavaScript Support
Currently, the action supports the built-in functions of node.js 8.11.1
. We can't import external libraries through npm
or so. Therefore, we can't rely on any require()
statement. Everything MUST stay in the action.
Inline JavaScript Code
Let's have a look at the JavaScript code below. It's not related to Logic App but the pure JavaScript code. If you run this code in a node.js console, it returns the latest file path value of 20191104.json
, which is expected. Array sorting feature in JavaScript needs a separate callback function, which contains the sort logic.
"use strict"; | |
var items = [ | |
{ | |
"Name": "20191101.json", | |
"Path": "/path/20191101.json" | |
}, | |
{ | |
"Name": "20191102.json", | |
"Path": "/path/20191102.json" | |
}, | |
{ | |
"Name": "20191103.json", | |
"Path": "/path/20191103.json" | |
}, | |
{ | |
"Name": "20191104.json", | |
"Path": "/path/20191104.json" | |
} | |
]; | |
var sorted = items.sort(function (a, b) { | |
var dateA = a.Name.replace('.json', ''); | |
var dateB = b.Name.replace('.json', ''); | |
// dateA is later than dateB: dateA gets the lower index. | |
if (dateA > dateB) { | |
return -1; | |
} | |
// dateA is older than dateB: dateB gets the lower index. | |
if (dateA < dateB) { | |
return 1; | |
} | |
// dateA and dateB is the same | |
return 0; | |
}); | |
var result = sorted[0].Path; | |
console.log(result); |
The callback function SHOULD return either -1
, 0
or 1
.
- Returning
-1
means, between the array itemsa
andb
,a
is sent to the lower index. - Returning
1
means the array itemb
is sent to the lower index.
Therefore, the callback function gets rid of .json
from the Name
property value of both a
and b
, compare both values to each other, and the later (larger) one goes to the upper place of the array item (takes the lower index). In other words, the array items are sorted in descending order.
If you want to know more about the sort, please refer to this page.
Now, let's apply this code into Logic App.
Inline JavaScript Code Action
Let's add an action for Inline JavaScript Code.
Then enter the JavaScript code in the action. It's almost the same as the one in above, but there are two places changed for accommodation.
"use strict"; | |
// Assign the array value from the output of the previous action, `List Backups`. | |
var items = workflowContext.actions.List_Backups.outputs.body.value; | |
var sorted = items.sort(function (a, b) { | |
var dateA = a.Name.replace('.json', ''); | |
var dateB = b.Name.replace('.json', ''); | |
if (dateA > dateB) { | |
return -1; | |
} | |
if (dateA < dateB) { | |
return 1; | |
} | |
return 0; | |
}); | |
var result = sorted[0].Path; | |
// Returns the result as output. | |
return result; |
- The
items
variable takes the array items from the output of the previous action,List Backups
. - In the last line, it uses the
return
statement to send the result of the action to theoutputs
value.
If we want to refer the result of this action, any action later in this workflow can use outputs('ACTION_NAME')?['body']
.
Comparison
Now, we only use this Inline JavaScript Code action and sort out the issue (pun intended). Let's compare the same result as the previous post, with the picture below.
The right-hand side is what we created in the previous post. At least we SHOULD use both the Select Filename from Backups
action (Select
) and the Take Latest Backup
action (Filter
). If we want a more elegant way, a few extra actions are placed before and after.
On the other hand, if we use the Inline JavaScript Code action, like the left-hand side, we only need one action.
But there's a caveat. Make sure that we have to have the Integration Account associated with using this inline code action. The Integration Account is pretty expensive and fixed price – US$ 302.4 (Basic) and US$ 986.4 (Standard) per month. If your organisation has already been using the Integration Account, then it's OK. However, if it hasn't, it SHOULD be really careful.
So far, we've walked through how to use the Inline JavaScript Code action to sort array items within the Logic App workflow. It's powerful but expensive. Therefore, only if your organisation takes the cost, use it.