Microsoft Flow Series – Recursive Flows

Need Recursion in Flow?

In a previous blog post Flows Calling Flows, we covered how we can have one Flow call another Flow using the scenario to return some information on the direct reports of a user. We now want to take it further and solve the challenge of returning the direct reports for a user, then all the direct reports of those direct reports, and again and again through however many levels of hierarchy there is in an organisation.

This is a common technique in programming and is called a recursive function. We can recreate a similar effect in Microsoft Flow, because as we learnt a Flow can call another Flow via an HTTP request and receive, so therefore we can do it recursively with a Flow calling itself.

Microsoft Flow Limitations

Obviously, recursion is not a native feature of Microsoft Flow and we need to be aware of some of the limitations when using HTTP requests to mimic recursion. All Microsoft Flow limitations can be reviewed here:

One important limit is the request timeout for synchronous calls, which means that an HTTP request which remains connected to receive data may not last more than 120 seconds. Using our scenario for recursion through the organisational hierarchy, from the first HTTP call for the manager through to the completion of all the recursive calls of all the direct reports, the total time cannot exceed 120 seconds. This will be important to consider in larger hierarchies.

There is a way to get around this limitation with the asynchronous calls, but that requires using webhooks to listen separately for the received data as and when it is received. The other important limitations are the concurrency/parallelism and rate limits, which we will cover in a separate post about optimisations.

Child Flow Calling Itself

In the previous example of a parent Flow calling a child Flow which the iterates through a manager’s direct reports, the user details of each found direct report are appended to a string variable. A new step (1) is added to the Flow, before the variable appending, which will result in the Flow calling itself.

The same child Flow URI is used in the HTTP request (1) and the current loop iteration direct report’s UPN is passed as the manager attribute. The returned data is also appended to the JSON variable.

This is what allows this Flow to traverse an organisational hierarchy. As it digs deeper into a branch, once it reaches the end point when a user has no direct reports, then the loop will not trigger, no further recursive calls to the Flow will happen and a blank string will be returned.

Organisational Hierarchy Using Flow

The end result below can be seen in the resulting JSON from the call to parent Flow. The first manager (1) has a direct report (2), who in turn also has a direct report (3) represented in the JSON.

Expand your knowledge of the Microsoft Modern Workplace, by subrscribing to our YouTube channel: