“Sometimes, when I use specific dynamic content, Power Automate adds the ‘Apply to each’ action automatically, how can I stop it? I don’t want to repeat any of the actions!”
You surely encountered this when building a Power Automate flow. You add the actions you need and use the available dynamic contents to configure them. It goes fine, until you add that one dynamic content. Once you add it, it’ll add ‘Apply to each’ around the action. And that’s where the problems can start. The flow will send multiple emails instead of one. It’ll create duplicate SP items instead of one item with multiple values. Or it could just stop working completely. Why does Power Automate do it and what can you do about it?
Why is it adding ‘Apply to each’?
Power Automate adds ‘Apply to each’ automatically every time you’re trying to access value from an array. As explained in the post on processing JSON, you can recognise arrays by the brackets [ and ]. Anytime you try to access anything inside [ ], Power Automate will automatically add ‘Apply to each’. It recognises it’s an array, and as such it wants to process all the objects/values. One by one, inside a loop.
That means, to avoid the ‘Apply the each’ you must extract the object/value from the array differently. You can’t leave it up to Power Automate.
Get rid of the array
You must extract the values from the array and turn them into a string, or select only a specific object/value from the array. There’re multiple options how to do it, depending on the array and how you want to use it.
If it’s a simple array, you can convert it into a string with the join(…) expression.
Array of objects (pre-processing possible)
If it’s an array of object, but you have the possibility to preprocess it, you can use the ‘Select’ action together with the join(…) expression.
Array of objects (more pre-processing needed)
Sometimes, for example when working with documents, you can’t use just ‘Select’. You need an extra action, e.g. ‘Get file content’ to extract content from each of the files separately. In that situation you’ll need a loop to preprocess it.
Array of objects (pre-processing not possible)
For some actions it’s not possible to pre-process the data, e.g. ‘Create HTML table’ or ‘Create csv table’. In such situations you’ll need the xpath(…) expression together with join(…).
Taking only a specific object
The last option is to take only a specific object from the array, e.g. the first approval response. If you take only a specific object from the array, the ‘Apply to each’ won’t be needed.
The same applies also if you extract the object/value directly from the JSON.
Power Automate will add ‘Apply to each’ each time you try to process an array and the only way to stop it is to preprocess the array by yourself. The actual processing depends on what you’re trying to achieve. If you want a specific value(s), you can use one of the first 3 options. If you want the whole object for later processing, the last options is what you’re looking for.
Just don’t forget to remove the automatically added ‘Apply to each’ at the end. Otherwise it’ll keep looping even after you preprocess it.