Checking if something is empty can save a lot of actions and potential breaking Flows. That’s why we have the “empty” function that will check if a collection is empty or not.
So let’s check how the “empty” function works.
Usage
It follows a simple pattern.
- String with a collection or,
- Collection
It will always return a true/false result.
Let’s start with a simple example:
empty(variables('ARRAY_VARIABLE'))
will return
true
The variable was initialized without any value, so the “empty” function returns that is indeed true.
Now, how about instead we provide:
empty('[]')
will return
false
Why? Notice that we’re not providing an array. Instead, we’re providing a string with one element. Since a string with one piece can be considered a collection, the result is false.
Let’s now check how it works with the “createArray” function, by creating an empty array.
empty(createArray(1))
will return
false
Makes sense. The “createArray” function creates an array with one element, so it’s not empty.
Finally, let’s provide an empty string from a variable:
empty(variables('EMPTY_STRING'))
will return
true
The empty function checks if an object is empty (remember the pack of cookies from before). It can check because we define the type, so the empty function knows how to check if it’s empty or not.
Limitations
Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise breaking it into smaller, manageable formulas.
Recommendations:
Here are some things to keep in mind.
Use “debug” compose actions.
Since the comparison will return true or false, sometimes it’s tricky to understand how the calculation is done, depending on how complex the expression is. So I recommend using Compose actions to have the values that go “in” the function. This way, if the value doesn’t make sense, you can understand, based on the parameters, why it was calculated that way.
Don’t nest
There’s no real reason to do it. The flow will allow you to nest “empty” functions in a formula, but then it will return the following error when it runs.
Unable to process template language expressions in action 'Compose' inputs at line '1' and column '4718': 'The template language function 'empty' expects its parameter to be an object, an array or a string. The provided value is of type 'Boolean'. Please see https://aka.ms/logicexpressions#empty for usage details.'.
The empty function cannot have a boolean as a parameter, as explained by the exception.
Sources:
Microsoft’s empty Function Reference
Back to the Power Automate Function Reference.
Photo by Tijs van Leur on Unsplash