Co-pilot, Flow, Power Automate, Uncategorized

Power Automate: contains(), array, AI and bananas.

Issue

I was modifying my custom environment creation automation today. For the security group creation action, I needed to add a System Admin to the security group owners to simplify a user management. The action takes the array like below:

[

"https://graph.microsoft.com/v1.0/users/24d2bbbb-ae82-4d3e-8520-081231b0edc0",
"https://graph.microsoft.com/v1.0/users/21d2aaaa-ae82-4d3e-8520-081231b0edc0"
]

First, I tried appending a System Admin value to the Owners array without making any checks. However, I was getting a Bad Request error suggesting that all item in the array must be unique:

“Object ‘[ResourceType=User,Id=xxxxxx-ae82-4d3e-8520-081231b0edc0,ChangedProperties=[],NonDefaultProperties=[INTERNAL_SingleResourceQuery]]’ referenced from ‘[ResourceType=Group,Id=77e2aab4-1b37-44ed-b4a7-85fd4ead461c,ChangedProperties=[DisplayName,Description,SecurityEnabled,MailEnabled,MailNickname,CreatedByAppId],NonDefaultProperties=[INTERNAL_EnableF14M1SchemaEnumTypes]]’ can only be present once as an add/remove change.”

I need a condition checking if user ID is already present in the array.

My rules

I don’t like too generic solutions.

  • I need to check for the array with max 3 items.
  • I only have strings in my array.
  • I need a partial matching for a user ID, if array item contains user ID substring.
  • I try use all tools available to me to solve the problem.

Wrong assumption

I started with the wrong assumption that I know what I am doing 🤞😎😎

I picked up the contains() function.

Spoiler: it worked at the end, but there are easier ways to achieve the same result.

Olena

Power Automate contains() function takes array or string as a first parameter. However, it works differently in each case.

For the string it will search for a second parameter as a substring. For example, if it searches in the string “I like bananas” fornana it will return true.

contains("I like banana", "nana") = true

For the array, it will search for the exact match for the item. For example, for fruits [ apple”, “pear”, “banana”] the contains(fruits, “nana”) returns false.

It may be fair but still confusing.

I wrongly assumed it will search for a substring, not for the exact string match for the array item. As soon as I realized my mistake, the issue was resolved.

Look!

Test run.

My solution

Let’s convert the array to string (as it is anyways)!

Test run.

It’s working!

Copilot (Bing chat)

The co-pilot was very helpful. When I already knew the answer. When I asked it explicitly “can I search for the partial match in array” it told me “Wait, NO!” While I was wondering around it wasn’t very helpful as by looking at the definition it wasn’t easy to figure out it works differently with different data types.

I tested another function indexOf.

Unfortunately, the co-pilot suggestion for using indexOf with array was totally misleading. When I tested it gave me an error.

Look!

Test run.

Converting array to string first, obviously, fixes the error.

Power Automate Copilot

“How do I search for partial match in array?”

“For the array on the step “…” I want to know if it contains a certain string”

It doesn’t fail, but the expression is wrong so it doesn’t work either.

Not helpful!

Google

The biggest “wrong” thing about any search I used not just Google – NO MICROSOFT DOCS REFERENCES. I don’t want your awesome YouTubers or community blogs! I want Microsoft learn article first. Please.

Google is still the best way to search.

The second from the top article gives you all sort of right answers:

https://www.powertechtips.com/check-if-array-contains-specific-value-power-automate/

Filter array action is the obvious one.

Conclusion

The main issue with teaching people to create a ‘correct’ prompt is that people don’t know what they don’t know. When I know how to ask the correct question, then my problem is almost solved. There are stages before when we are forming a dictionary, finding out how to name things correctly for the area with which we are not familiar.

Co-pilot is more helpful than Power Automate co-pilot. Neither solved the problem though.

Google is still the best way to search for answers.

It helps when Microsoft documentation links in the search results appear before all popular videos and community blogs.

1 thought on “Power Automate: contains(), array, AI and bananas.”

Leave a comment