user22062628
user22062628

Reputation: 3

How to get rid of "apply to each" on Power Automate (sending only one email)

I have been working on Power Automate. My goal is to have it run automatically everyday to check if there's any file older than 7 days in my OneDrive or not, if yes, it will delete all those files and send an email (Outlook) to me.

So far, it does what it supposed to do. Except, the send email action is in the loop (apply to each). So if I have 500 files, it will send me 500 emails. I looked up online and saw that some people use initialize variable and compose to avoid the loop. I only need the flow to send me only one email with the list of files it has deleted.

My problem is if I took the send email action out of the loop, it will be disconnected from my if/else condition statement too. This will cause the flow to send an email whether there is a file deleted or not.

enter image description here

However, if I initialize a variable, I'm not sure what to put as the value? If I want to include all 500 files' names in one email and send it out to the user. Any suggestion on what I should put as value in here?

enter image description here

Any suggestion is appreciated. I have looked at a bunch of solutions and suggestions online but they're mostly used with SharePoint or some other apps, and they have different kinds of values/fields

Upvotes: 0

Views: 1896

Answers (1)

jleture
jleture

Reputation: 1088

You have almost find the solution!

Yes, you have to initialize a variable (type string, or, better, array), edited this variable on loop (concat if string, or append if array).

The email action must be outside the loop (like your first screenshot). But you have to add a condition to check the variable value:

  • if no value, do nothing (or add a Terminate action)
  • if there is some value (length of the array variable greater than 0), you can send the email.

Fix to apply:

Change the variable type to Array

variable array

In the loop add a Append to array variable action to add the filename

append array

After the loop, add a Condition action: length(variables('varFile')) is greater than 0

condition

If yes, send and email and get the filenames with the expression join(variables('varFile'), ';')

join

If No, add a Terminateaction with status Cancelled

Upvotes: 0

Related Questions