Reputation: 3
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.
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?
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
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:
Terminate
action)Fix to apply:
Change the variable type to Array
In the loop add a Append to array variable
action to add the filename
After the loop, add a Condition
action: length(variables('varFile')) is greater than 0
If yes, send and email and get the filenames with the expression join(variables('varFile'), ';')
If No, add a Terminate
action with status Cancelled
Upvotes: 0