Power Automate Apply To Each Action In Workflows

by | Power BI

In this tutorial, we’ll discuss another control action that we can use in our Microsoft flows. There are triggers that can immediately start a flow based on an event such as when there’s a new email or tweet. These triggers are great, but there are flows that we’d also want to run through a loop, and take certain actions on each item. To do this, we can use the Power Automate Apply To Each action control in our workflows.

Sample Scenario

Let’s say for example, we have a flow that contains a Switch control. And in our case number 3, the action is to post a slack message to a specific channel.

Power Automate Apply To Each

However, what if we want to loop through all channels and post the specific message to all of them? 

The first thing that we need is a list of all the channels. Then, we need to apply the same action on all of the said channels. That’s what the Power Automate Apply To Each control does.

Apply To Each control loops through an array of items and it performs an action on each item.

So, let’s now delete the Post message action.

Power Automate Apply To Each

Using The Power Automate Apply To Each Control

First, let’s add a new action for Case 3 (when the delivery method is equal to Slack).

Power Automate Apply To Each

Next is to get a list of all the available Slack channels. We currently have 5 channels on our Slack. 

Power Automate Apply To Each

In our case, let’s search and click Slack.

Power Automate Apply To Each

Click on the List public channels.

Power Automate Apply To Each

This doesn’t require any inputs. This is only used to get access to an array called Channels that we can then loop through. 

Let’s now add another action.

Click Control.

Power Automate Apply To Each

Then, click on the Apply to each control.

Power Automate Apply To Each

This control takes two properties. The first one is an array of items that it needs to iterate through. Our array of items will simply be the list of channels. Therefore, let’s select the Channel Name variable. 

Power Automate Apply To Each

The next thing that we need is the action that needs to be applied to each of these items. So, let’s add another action.

Power Automate Apply To Each

Search and select Slack.

Power Automate Apply To Each

Click Post message.

Power Automate Apply To Each

Then, let’s make the Message Text be the actual message itself.

Power Automate Apply To Each

For the channel name, select the Channel Name variable.

Power Automate Apply To Each

If we choose Slack, the flow will go onto Case 3 where it will get us a list of all the public channels. It will then loop through all those channels one by one, and perform the corresponding action.

Testing The Apply To Each Control In A Power Automate Flow

Let’s now test our flow and see if it works.

Choose the “I’ll perform the trigger action” option and click the Save & Test button.

The delivery method should be Slack and the message should be “This should be posted to all Slack Channels”. Then click the Run flow button.

Once the flow has successfully started, click Done.

We chose Slack as the delivery method, so the flow went directly to Case 3. Within the action, we’ll see the channel name and message text for the first iteration.

Let’s click Next to see the second iteration.

The third one is general.

After that is the budget channel.

Last but not the least is the discord channel.

Upon checking Slack, we’ll see that the message was posted in all the public channels. Therefore, the loop worked properly.

Using The Current Item Variable In Apply To Each Control

In some cases, we might need to save the properties of all our slack channels and email them out. For that, we can use the Current item variable instead.

For example, let’s add an action.

Search and select the Mail connector.

Click the Send an email notification (V3) action.

Set the email address to where you want it to be sent. Then add the Subject. For the content of the email, select the Current item under the Dynamic content tab.

This loop will go through the first channel and post the message there. Then, it’ll send an email with the JSON file of that channel. With this, we can learn things about the channel such as the channel ID, when it was created, and the like.

***** Related Links *****
Power Automate Trigger Conditions – An Introduction
Workflow Inputs In Microsoft Power Automate
Power Automate Expressions – An Introduction

Conclusion

And that’s how the Apply To Each control works. We can use this control to navigate and process all the items in an array or a list. This is one of Power Automate’s most useful actions.

Always keep in mind that whenever you use this control, you’ll need two properties or arguments. The first one is an array of items and the other one is the action. You can add multiple actions inside this action control but as always, keep them to a minimum to avoid slow-running flows.

All the best,

Henry

author avatar
Henry Habib
Henry Habib is an accomplished Power Platform and Office 365 trainer, with over 100 hours of recorded content and over 30,000 paid students on e-learning platforms.

Related Posts