In Power Automate, we need to parse a lot of strings and collections and, sometimes, it’s essential to understand if we have something that exists inside another collection. Power Automate has the “contains” function that helps with this.
For example, if you want to go through a list of people and get the unique names, you would need to:
- Define an array variable
- For each element
- If it exists in the collection, add it to the array.
It’s a simple example, but it would be a lot harder without the “contains” function.
So let’s see how to use it.
It follows a simple pattern.
- Collection (either a string or an object)
- Value to search
Let’s start with a simple example:
createArray('1','2','3') contains(variables('TEST_ARRAY'),'2') will return true
It makes sense since two exist somewhere in the collection. But how about if we provide the number “two” instead of the string “two”?
createArray('1','2','3') contains(variables('TEST_ARRAY'),2) will return true
The number will be converted to a string and then compared. Speaking of comparisons, let’s try with spaces and see what we get.
createArray('Manuel','T','Gomes') contains(variables('STRING_ARRAY'),' T ') will return false
The string needs to match the absolute value. So let’s try the opposite way.
createArray('Manuel',' T ', 'Gomes') contains(variables('ARRAY_WITH_SPACES'),'T') will return false
Again, the string needs to match with the exact same characters.
Let’s try with boolean values:
createArray(true,false) contains(variables('BOOLEAN_ARRAY'),true) contains(variables('BOOLEAN_ARRAY'), false) will return true
How about this?
createArray(true,false) contains(variables('BOOLEAN_ARRAY'),'true') will return false
Why? The datatypes are different, so the comparison fails. Second, the behavior is a bit different from integer values like we’re seen above.
Finally, let’s test another collection.
createArray('Manuel','T','Gomes') contains(variables('STRING_ARRAY'),'["Manuel","T","Gomes"]') will return false
Although they have the same structure, Power Automate will consider the second parameter as a string and not an array. However, notice that we can use it safely on the right-hand parameter, considering it as an array.
contains('["Manuel","T","Gomes"]','Manuel') will return true
You can replace all notations of the createArray function with the string as above, and it will work the same way.
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.
Also, you can’t compare arrays with this method, and you should be careful with the conversions, as we’ve seen above. But, again, this is because different types behave in another way.
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.
There’s no real reason to do it. Furthermore, if you try to nest “contains” functions, you’ll receive an error since we don’t have a collection to compare. So be very careful to avoid breaking Flows.
Microsoft’s contains Function Reference
Back to the Power Automate Function Reference.
Photo by Michael Dziedzic on Unsplash
2 thoughts on “Power Automate: contains function”
Hi, can the contains function be used in the below scenario? I tried different various and all return false…
Searching in JSON arrays is hard with the “Compose” function, but you can use the Filter Array. I’ll write an article in the future, but you can use the Filter Array action with an equals like this:
You’ll get the following:
Can you please try and let me know if it works?