Introduction to Loops in Power Automate
Do you want to create Power Automate flows that loop through multiple items? In this short clip from my Automating Business Processes Using Power Automate course on Pluralsight you will see how to use the Apply to each loop in a real life example!
FULL Course on how to automate business processes with Microsoft Power Automate (opens in a new tab)
Video Summary
Here are the key points from the video:
- Introduction to Loops: Loops in Cloud flows repeat actions until a condition is met. There are two types: “apply to each” and “do until.”
- Apply to Each Loop: This loop processes multiple items at once, often used in scheduled flows. For example, it can check items in a SharePoint list nightly and send reminders if due dates are past.
- Do Until Loop: This loop repeats actions until a condition is true. For instance, it can send daily reminders for pending approvals until the process is complete.
- Demo of Apply to Each Loop: The demo shows creating a flow that gets items from a SharePoint list and sends emails for high-priority items. Power Automate can automatically insert loops when needed.
- Flow Execution: The flow is manually triggered, retrieves items, checks conditions, and sends emails accordingly. The demo confirms the flow works as expected by showing the sent email and loop details.
For more information, read the transcript blog below, or watch the video above!
Video Transcript
Our next logic control that we will talk about is loops. So, what are loops? Loops are a block of actions that will repeat over and over again until a condition is met. There are two types of loops that we can use inside Cloud flows. The first one is an “apply to each” and the second one is a “do until.” Let’s take a look at them in detail.
Let’s start with the “apply to each” loop. The “apply to each” loop is useful when you want to process multiple items at once, and it’s very often used for instant or scheduled Cloud flows. To give you an example of how we can use it, we could have a flow that runs every night and checks all of the items in a SharePoint list. So, we would do a “get items” and then an “apply to each” loop on each one of those items to see if the due date is higher than today, and if yes, send a reminder.
Next up, we have the “do until” loop, which allows you to repeat a set of actions until a certain condition is true. So, do this until the condition is met. If we look at an example, think of an approval flow, and while the approval is pending, send a daily reminder to the approver until the approval process is complete.
Now that we have seen the “apply to each” loop theory, let’s head over to the lab environment and see how we can loop through multiple items with the “apply to each” loop. We are now in the lab environment. Let me open up the browser. Here, we are going to use the same work progress tracker list and let’s go to Power Automate. Let’s do a new flow and we will do an instant Cloud flow. We will give it a name and we will call it “Loop Demo,” and our trigger will simply be to manually trigger this flow. So, what I will do is manually trigger a flow, and then I will add a new step, which will be “get items” with an “s.” So, we will get all of the different items from this SharePoint list. Now, let me get the site address. Here, it will be the “Getting Started with Power Automate” site, and then the list name will be the “Work Progress Tracker.” So far, no loops involved, but now we want to send an email for each of the items that, let’s say, have a high priority. So, let me go back into Power Automate. There are two ways to add a loop. First of all, you can always go into control and then add an “apply to each” loop, and then it will ask me to select an output from the previous steps, and here we’ll select the value. Now, the value is not clear really from the name on what it actually returns. However, the description “list of items” really tells us that this is the good one.
After that, the other way to do it, and I’ll just delete this for now, you will see that Power Automate is pretty smart. If I actually just do a “send an email” here, and then it will ask me, let’s say I use in the body, I use something such as the “TP title,” for example, Power Automate will automatically say, “Hey Vlad, you want to send an email with the title, but the dynamic content you just retrieved is multiple items.” So, what I’m going to do is I’m going to automatically insert an “apply to each” loop for you because you have multiple items in there. So, that is the second way of doing it. It’s a really nice feature that makes sure users have fewer errors when they deal with Power Automate and multiple items. So, I just wanted to show it to you.
So, what we’ll do now, I’ll just delete this actual “send email” for now. It will keep our “apply to each” loop on the value, so same dynamic content, but then what I have to do is I actually need to get the item detail, the one that I’m looping on. So, what I will do here, let me give it the site address again. I’ll get the “Getting Started with Power Automate” site. The list will be the “Work Progress Tracker,” and then under “get items,” I will select the ID. Great. Then let’s add a quick condition here. I will say if the priority equals high, like so. Let me select my priority value equals high. Then I will send an email. So, let me add an action here. Let’s search for “send email” here. Office 365 Outlook, send an email to Vlad at Global Mantics dot org, and then we’ll say “high priority,” and then let’s take the title from the “get item,” and then in the body, we’ll just add the description. Awesome. Let me save this flow, and while it’s saving, I will just go in the presentation slides here and then change the priority to high, so we actually have one that will trigger the email. There we go. Let’s see if the flow saved. It’s saved. Let’s go back to the flow and run it. Now, because it’s an instant flow, I can run it right away. Let’s click on continue and then run the flow. It will take a few seconds. Let’s go inside and see what happens. It already finished. If everything goes well, we have one email, “high priority presentation slides,” and if I go to SharePoint, that was the title, and the description was “PowerPoint slides of the presentation,” which is in my body. So, that worked well, but let’s take a look at how that looks like from the Power Automate side. So, I got my “get items” here, and then I’m doing an “apply to each” right away. You can see that the “apply to each” will show me how many times it looped. So, right now, it’s showing me the first item out of three, and then I can say, “Okay, the condition for this one was valid.” If I go to the next one here, so I go on next, it will show me the second item it looped on. I’ll scroll down. I see all of the details, and then this one probably, let me actually minimize this because it’s showing us quite a few details here, makes me scroll down or up forever. Let’s go to the “get item.” Okay, the condition for this one was false, and the same thing for the last one since we only had one single email. But as you can see, this is how you can do an “apply to each” loop in Power Automate.
So, this is it for this quick demo in which we have gotten all of the items from a SharePoint list, looked through each one of them, and depending on a condition, we sent an email or did nothing if it didn’t meet our condition. This is it for this demo. Now, let’s head back to the slides and learn about branches in Power Automate Cloud flows.
