Maintain clean and reusable code in Power Automate Desktop Flows with Subflows

Power Automate Desktop - Solutions

You’re tasked with creating a Power Automate Desktop (PAD) Flow that contains many different stages, you have noticed that your Flow is getting incredibly long, how do you maintain a clean-code approach? Maybe you want to reuse a section of your Flow rather than copying and pasting it multiple times, how do you do this? Well this is where Subflows come in to play!

What is a Subflow?

A Subflow effectively is a Flow, it’s the exact same ‘code’ that you would use within your normal Power Automate Desktop Flows, however, you’re placing it into a container that can be called multiple times. This can be seen by the image below, it shows an initial PAD Flow, then splitting that Flow up into sections and those sections being called.

Essentially, we’re enabling ourselves to write contained Flows within one PAD Flow.

How do we use Subflows?

When you launch PAD, you’re presented with the Flow Workshop Designer. At the top where the Save, Run and action buttons are, just below those are your Flow Tabs. Here you will see ‘Main’ which is your main Flow and there’s also a Subflows dropdown.

Here, if you select that dropdown you’re presented with a list of your Subflows and the ability to add a new Subflow.

Clicking this will add another tab, which is a Subflow, that you can begin writing your Flow code into.


Let’s look at an example of this. I’ve created a basic PAD Flow that creates a list of ‘Names’ and adds three items to that list. I’m also setting another variable called ‘Iteration’ to 0, to track our progress through a foreach loop for our list index.

I then have a foreach loop that cycles through each of the items in the ‘Names’ list. If the name is not equal to ‘FlowJoe’ then I will remove that list item. If it is ‘FlowJoe’ then I’ll increase my ‘Iteration’ variable to move onto the next index.

However, in this scenario, we need to add another name to the list and rerun our foreach loop (we are using this as an example of data changing). Currently, this logic is not in our scenario. I could just copy and paste the foreach loop after the other name ‘Sarah’ is added, further increasing my lines of code in this situation and duplicating work.

You can see this below:

So, we have a lot of information including variables, loops and edits to data after we have checked it. Lets take a look at how we can better manage this Flow, clean it up and reuse some of the code.

Firstly, let’s take a look at the variables. The variables will be moved into a Subflow so that it’s easy for us to access each of these variables if anything needs to be changed. Let’s then create a the ‘SetInitialVariables’ Subflow.

To do this we will select the dropdown menu that we saw previously, and select ‘New Subflow’.

You will be presented with an ‘Add a Subflow’ popup, enter the Subflow name (Subflows cannot contain spaces within their names) and click save.

We now have a tab ‘SetInitialVariables’ that is automatically opened and displayed to us.

Now we can drag and drop the variables from our ‘Main’ tab to our ‘SetInitialVariables’ tab. To do this simply click the first item, hold shift and then select the last item, hold your left mouse button down and drag those items over to the other tab, wait for the tab designer window to load and drop them into the main area. In our situation. we’re just taking ‘Create New List’ and ‘Set Variable’ to our new tab.

We then want to copy our foreach loop into its own Subflow as we will be reusing the loop. Lets create another Subflow called ‘ListChecker’ and then drag the foreach loop into that tab.

We now have two Subflows ‘SetInitialVariables’ and ‘ListChecker’ alongside our ‘Main’ Flow.

Lets head back to our ‘Main’ flow. Currently we only have the ‘Add item to list’ actions. This PAD Flow will fail as our variables aren’t being set. We need to call our Subflows for them to be used.

You can find the ‘Run subflow’ action in the ‘Flow control’ section of the actions list.

Drag this to the top of our ‘Main’ flow. You are presented with a popup window that asks you to select the Subflow you wish to run. In the dropdown, select ‘SetInitialVariables’ and click save.

You will now have a ‘Run subflow’ action that targets the ‘SetInitialVariables’ Subflow.

We now need to do the same but for the foreach loop Subflow ‘ListChecker’ and place it above the ‘Sarah’ add list item (remember we’re using this as an example of data changing).

Now we can reuse our foreach loop, as we have moved it into a Subflow. We wanted to add the same logic after ‘Sarah’ was added to the list. Now we can, by simply calling ‘Run subflow’ again after ‘Sarah’ has been added to the list. We do not need to copy and paste code, duplicating unnecessary code, we’re reusing it.

Our Flow now looks like this:

We do however have to reset our index iteration variable back to 0.

We have taken variables into a Subflow so they can be easily managed, we have taken logic that we wanted to reuse into a Subflow and we have used it twice. We have practiced clean-code by reusing and not duplicating, if we need to change the logic for our foreach loop, we only have to change it once, not multiple times. Our ‘Main’ Flow is now easily readable and maintainable. Future considerations; you can move the data into its own Subflow, you can also reset the index variable in another Subflow.

You can click the GIF above to see the run through and Subflow steps alongside the desired outcome of just ‘FlowJoe’ being left in the list of ‘Names’.