Arrays are part of the building blocks of Power Automate. We can get arrays from many actions like the SharePoint “Get Items” action, for example. But there are a lot of times that we only want the first element of the array. So that’s where the Power Automate “first “function comes into play.
Let’s see how to use it efficiently.
It follows a simple pattern.
- Array to parse
Let’s start with a simple example:
createArray('Manuel','T','Gomes') first(<array>) will return 'Manuel'
How about if the array is empty.
You’ll get an error message as follows:
Unable to process template language expressions in action 'Compose' inputs at line '1' and column '6310': 'The template language function 'createArray' expects a comma separated list of parameters. The function was invoked with no parameters. Please see https://aka.ms/logicexpressions#createArray for usage details.'.
You always need to check if the source array is not empty. If you don’t check, your Flow will fail. Here’s a skeleton on how to do it.
We’re using the createArray function to generate a test array, but you’ll probably use another action in your case.
Finally, let’s check if it’s a string. We can do the following:
first('Manuel,T,Gomes') will return 'M'
Here you can consider the string as an array of characters. The Power Automate “first “function will return the first character of the array. The same happens if there are spaces in a string.
first('Manuel T Gomes') will return 'M"
I could not find any limitations for this function, but please let me know if you do.
Here are some things to keep in mind.
Always check the array.
In my opinion, the “first” function would be a lot more valuable if it returned an empty array when the source array was empty instead of an error. By returning an error, we need to validate if the array is not empty; otherwise, the Flow will fail.
It’s not a big deal, but be sure to validate the data to avoid having failing Flows.
There’s no real reason to do it, but if you find yourself in a situation where you have nested first functions in a formula, you should review it and make everything more straightforward.
Back to the Power Automate Function Reference.