arraysobjectforeachsharepoint-onlinepower-automate

Power Automate keeps adding an "Apply to Each" and has nested my "Send an Email" action so far down that I'm only getting errors


I'm creating a flow that would send a notification to users based on timeframes that I've set up in individual variables - however, to test that, I have to run through the entire flow. Therein lies the problem.

The flow is pulling data from a SharePoint Online List, users are being used from two different columns and used as email recipients of the notification messages. However, each time I add the dynamic content, P.A. creates a new "For Each" loop and nests my "Send an Email" action within that action.

See below:

This is the full Flow Full Flow

This is the Branch I am currently working on 15 Days Out Branch

Here is where I added the first recipient email address and Power Automate automatically added "Apply to Each" Added Lab Director Email

Added the second recipient email address and the second "Apply to Each" action was automatically added which will cause the flow to error out. Added Responsible Person Email

This is the error I get when I run the Test:

Test Error

That's pretty much the extent of the flow, calculate 15, 45, 90 and Expiration Date of the given column and send an email to those recipients - if I can only get over this hump of my email addresses not splitting up in "Apply to Each" actions. If anyone has any experience with this, I would be greatly appreciative! Thanks!


Solution

  • What causes Power Automate to wrap your action in Apply to Each is that your Person or Group field is set up in a way that multiple users can be selected. So Power Automate readies your flow to handle multiple values.

    The other issue is that in many cases you only have one value in those fields so at runtime Power Automate is facing a single Person object instead of an array of Person objects.

    Here's what you can do:

    1. Initialize an array variable for all recipients.
    2. Use Apply to Each loops to iterate the Directors and Responsibles and Append to array variables inside to add them to the array variable. This makes sure that you have an array type even if there's only a single recipient.
    3. Use the Select action to reduce the Person objects to their email addresses.
    4. Use the Join action to concatenate all the email addresses. Use semicolon separator.

    Now you have a single string value with all the email addresses so you can send a single email in the end with all recipients in the 'To' field. This article clarifies the Select/Join part.