“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.
Simple array
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, but at least you can make them a bit faster.
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.
Summary
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.
Hi Tom,
I’m so close to completing my whole app ..last part is the email to be sent when a SharePoint list item if modified with this type of flow. I just don’t seem to get it ( limited skill set) i suspect. My flow is still sending multiple mails to one email address. I have just one multi-select column i am referencing. I also have an attachment array that I’m using to ensure the attachments are added to the email which is working for the most part
I cant seem to get this to stop the multiple mails. I have read you posts about using string variable instead or using Select, but just cant get it right..also i cant really afford your consulting fee… Is there any way of possibly steering me into the right direction?
Thank you
yours in Power Automate hope
Laurence
Hello Laurence,
check these two articles, they should show you how to handle multiple selection fields and multiple attachments: https://tomriha.com/how-to-convert-array-to-a-string-in-power-automate/ and https://tomriha.com/send-an-email-with-multiple-0-n-item-attachments-in-power-automate/.
Hello Tom,
Thank you very much for this article. I ran into exactly this issue of the apply to each sending multiple emails where more than one file upload was being processed. Using Append to string variable to capture the file link and moving the email out to the next step has solved it for me. Much appreciated, I’ll be checking some of your other articles.
Thanks Mike
Hi Tom, great tips!
From my experience, you CAN use “Select” to remap “Get Files (properties only)” in order to attach multiple attachments. It looks like:
————————————————————-
SELECT
From: value (of Get Files)
Map:
Name File name with extension
ContentBytes Identifier
————————————————————-
Then, in the Send Email connector, change the attachments icon to accept an array, and choose the outputs of the Select action. Works for me so far!
Hello Brian,
thank you for sharing, you’re right, you can define the whole object in the Map field.
Hi Tom,
Thanks for the informative article. Not exact scenario as you described, but is there a way to avoid the “Apply to Each” loop if I want to convert a nested JSON array to a tabular format (not concat multiple values into one cell, but save each value as a record in a tabular format so the data can be loaded into a sql table for example)?
Hello Ricky,
you can use the array from the JSON as the input of the ‘Create CSV’ or ‘Create HTML table’ or ‘Select’ actions.
Hi Tom,
My flow slightly different, i want the flow to send notification email when a field in SP List changed the status (e.g. field ‘Review Status’ = ‘Completed’), the email should send to a single person field (e.g. ‘Supervisor’) and a multi-person field (e.g. ‘Team Members’). I used Automated Cloud Flow: When an item is created or modified > Get changes for an item or a file > ‘Condition’ (‘Review Status’ = ‘Completed’) > If Yes > Get items > Apply to each (Output is from the Get items value) > Compose (Input is the multi-person field ‘Team Members’ ) > Select (From is the Compose output, Map is the Email of Team Members) > Join (From is the Output of Select; Join with is semicolon ‘;’) > Send an email. The flow works well if user edit 1 line item, but when user edit more than 1 item (e,g, edited 2 line items at the same), the flow triggered 2 same emails per item (total 4 emails sent). Any advice how do i fix the duplicate email? Thanks!
Hello Swan,
not really, as the flows are not connected to each other each item update will trigger its own flow… some kind of a workaround could be a scheduled flow that’d send the updated items once per hour.
Hi Tom
This looks to be similar to what i have but not quite.
My scenario is i have address fields coming from a HTTP request, which seem to be in an array (1st line, 2nd line postcode etc). I need to be able to map these dynamic fields in a Populate a Microsoft Word Template action, but every i add one of this address fields to the template im getting the Apply to Each issue. I’m not a coder, all self learning and clicking out. Any help on how i can stop this from happening would be much appreciated
Hello AS,
take a look on this article, I’m explaining how to pick a specific piece of information from JSON using expressions where you can bypass the loops using indexes: https://tomriha.com/how-to-get-a-specific-value-from-a-json-object-in-power-automate/