matryoska

Create recursive flows in Power Automate

With the introduction of child flow, have you tried creating recursive flows?

Cannot create recursive Child Flow

Recursive flows

Before looking into how to implement recursive flows, please review my child flow error handling post. If you implement recursive flows without the proper error handling in place you will cause some troubles! But more about that later in this post.

What is a recursive flow?

When you use child flows there will be a parent flow and a child flow. The Parent flow calls a child flow and then will get a result returned by the Child flow. Now if you get the child flow to call itself you will find that you get the following error when you try to save your flow:

Request to XRM API failed with error: ‘Message: Flow client error returned with status code “BadRequest” and details “{“error”:{“code”:”FlowCannotInvokeItself”,”message”:”The Flow could not be saved because Flows cannot invoke themselves as Child Flows.”}}”. Code: 0x80060467 InnerError: ‘.

Ok, this is a bit of a pain.

In my case I wanted to scan through folders in SharePoint and I create a flow that would scan a folder and its sub folders. The obvious way to implement this would be to have a main flow that would call the Scan Folder Flow and the Scan Folder Flow goes through all the subfolders and calls the Scan Folder Flow on the subfolders, until folders without subfolders are found and then the flows will end.

Make recursive flows it work

I’m going to create a Child flow that will take a set of numbers. the main flow that is calling this child flow will look like this:

Create recursive flows in Power Automate Microsoft Power Automate image 34

Note that before you can create the above flow you will have to create your child flow first.

Creating the Child Flow

My child flow will take the list of number supplied. In my flow below I’m setting the first Compose to my Input Parameter.

Then I’m finding the first number in the list. Before I’m going to set a variable to the left over numbers.

So as my main flow is feeding this child flow with 1,2,3,4,5,6 we should find that the first number is 1 and the leftovers are 2,3,4,5,6.

So far so good. Nothing complicated there.

Create recursive flows in Power Automate Microsoft Power Automate image 35

Now I want to call the child flow again with the leftover numbers. We already know that the child flow cannot call itself, but it can call another flow! In my case I’m going to call this flow Helper Flow. This helper flow isgoing to take the content of my leftovers variable.

Recursive flows with a help flow

The Helper Flow

The helper flow is simple. It takes the parameters that we want to give to the Child flow and then it calls the child flow.

Create recursive flows in Power Automate Microsoft Power Automate image 37

And that is it!

So even though we cannot create flows calling themselves, it is possible to create a recursive process by putting a helper flow in between.

Returning a response

I already promised a few warnings.

First of all the child flows have to return with a Respond to a PowerApp or flow action. If they don’t you will find that the calling flows remain running as they are waiting for a response. Using the error handling try catch pattern as mentioned in the child flow error handling post becomes critical!

It is also very important that there is an end to the chain of flows. Recursiveness only works if the workload reduces with every step deeper into the chain of child flows.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: