November 20, 2024

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:

  1. Define an array variable
  2. For each element
  3. 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.

Although the function is used a lot with variables, it can be used in any collection.

So let’s see how to use it.

Usage

It follows a simple pattern.

  1. Collection (either a string or an object)
  2. 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.

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.

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.

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. 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.

Sources:

Microsoft’s contains Function Reference

Back to the Power Automate Function Reference.

Photo by Michael Dziedzic on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

2 thoughts on “Power Automate: contains function

  1. Hi, can the contains function be used in the below scenario? I tried different various and all return false…
    [
    {
    “id”:1,
    “nombre”:”Kate”
    },
    {
    “id”:2,
    “nombre”:”Ana”
    }
    ]

    1. Hi Liz,
      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?

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon