Managed Metadata fields in SharePoint are a powerful way to distribute common metadata across your environment. When you work with Power Automate, you need to be aware of some limitations (at the moment).
I have a love/hate relationship with Power Automate. On one hand I love the capabilities it gives people when working with Office 365 data, on the other hand I hate how complex things can be for setting up easy tasks. With a history of using SharePoint Designer or Nintex Workflow, I guess I am just spoiled with how easy it can become for a non techie end user to create business processes.
In this case, I encountered an error while working with the Managed Metadata field where the field is set to have multiple values.
Working on a project for a customer, I encountered this error. What happened was this:
A document library got a metadata field of the type “Managed Metadata”, with the property that it could have multiple items. Let’s say: a list of countries to make it an easy example. The flow in Power Automate needed to grab those values (it could have multiple values, so they are stored in an Array) and do something with it.
The problem with the flow was that it was failing every time someone dragged a document in the library.
Action ‘Apply_to_each’ failed
The error message was this: Action ‘Apply_to_each’ failed.
Details: The execution of template action ‘Apply_to_each’ failed: the result of the evaluation of ‘foreach’ expression ‘@triggerOutputs()?[‘body/Location’]’ is of type ‘Null’. The result must be a valid array.
In other words: the action was expecting an array, but when the metadata field is not filled in, it doesn’t create an array resulting in a NULL. And when you drag/drop a file in the library, SharePoint doesn’t ask you for metadata, hence the error.
Fix: Power Automate Trigger Conditions
There are probably tons of different ways how you can fix this error, I tried a few ways to get out of the way of the action but in the end, the easiest one for me was to simply not let the flow run when the field was empty.
You can do that by using the trigger conditions: setting up a condition that needs to be true for your flow to start running. Here is a way on how to come up with that formula in an easy way. As I was using the “when a file is created or modified” trigger, I could set up a condition:
You simply go to the trigger, open up the settings and on the bottom you can insert one of those unreadable formulas that has to equate to a boolean value of True.
Thankfully there is an easy way to come up with that formula by using an “initialize variable” action and using the formula builder to come up with that formula by itself.
The result is that when the managed metadata value for the item is empty (when we drag a document in), the workflow will just not fire. As a bonus, this will also limit the amount of Flow runs!