Power Automate: Building a Parent-Child Flow

Introduction

In this article, we will see how we can create a Parent-Child Cloud Flow implementation. With the wide adoption of cloud flows, complex business processes are being migrated to Power Automate which often leads to 100s of actions in the flow that eventually makes the flow unstable and difficult to maintain. Also, there would be situations where we would have common logic across different processes that can be clubbed together in a child flow. In such situations, isolating/moving the logic to a child flow and calling it when needed from the parent flow results in better implementation and maintainability of the cloud flows.

Business Use Case

To demonstrate the Parent-Child Flow implementation let’s consider a scenario where we have procurement details pertaining to different departments are stored in individual lists and the requirement is to run a scheduled flow every week to calculate the procurement costs in multiple departments and share the summary to the Finance Controller.

To achieve this, we can break down the logic into a Parent-Child Implementation where:

  • Parent Flow – Created as a Scheduled flow that runs every Friday and maintains the names of department lists where the procurement details are stored. For each department list, it calls the child flow.
  • Child Flow – As the logic to calculate the weekly procurement costs for department is same, we maintain this in the child flow so that it can be run for various departments and total procurement cost can be returned to the Parent Flow for further processing

Implementation

To demonstrate the implementation, we will create Lists for 2 Departments Travel and Marketing with the same fields as below:

Background pattern

Description automatically generated with low confidence

Travel Procurement List

Table

Description automatically generated

Marketing Procurement List

Graphical user interface, table

Description automatically generated

Now let’s get started with the Cloud Flow creation. The Parent and Child flow will be created as a solution. So, head over to the Solutions tab and click on New Solution which will house the Parent and Child Flows. Specify a name for the solution

Graphical user interface, text, application, email

Description automatically generated

Let’s create the child flow first, which will be called by the parent flow on every Friday schedule to calculate the weekly procurement expenses. Select Automation -> Cloud Flow -> Instant as the flow type. We can also use the Power Apps or the When an HTTP request is received triggers for child flows. For now, we will go with a manual trigger.

Graphical user interface, application

Description automatically generated

Select Add an input and enter the Input parameter name. The input that we define here will be passed into the child flow from the parent flow. Since we are passing the List name on which the processing will happen, let’s name the input as ListName.

Graphical user interface, text, application, email

Description automatically generated

We will also create two variables to hold the Week Starting and Ending date so that it can be used to filter the data returned from the SharePoint List.

  • Week Start Date Variable

The week start date can be calculated by subtracting 5 days from the current date (Friday) on which the flow would be running giving us the effective Monday of that week using the below expression :

Expression : addDays(utcNow(),-5,’yyyy-MM-dd’)

Graphical user interface, application

Description automatically generated
  • Week end Date Variable

The Week End Date will be the current date(Friday) of the flow run which we can get using the expression: utcNow(‘yyyy-MM-dd’)

Graphical user interface, application

Description automatically generated

We will also initialize another variable TotalWeeklyExpenses which will sum the totals of the expenses for each item that was procured this week.

Graphical user interface, text, application, email

Description automatically generated

Once we have the date variables in place, we can get the filtered items from the SharePoint list(whose name was passed in by the parent flow)by comparing the Procurement date with the Start and End Date of the week so that the action returns only those items that were procured in the current week :

OData Filter Query : ProcurementDate ge ‘@{variables(‘WeekStartingDate’)}’ and ProcurementDate le ‘@{variables(‘CurrentFridayDate’)}’
Graphical user interface, text, application, email

Description automatically generated

Once we have the data returned from the list, we will loop through the returned list items and add a compose action to get the value of the cost field of each row in the iteration using the formula

add(int(variables(‘TotalWeeklyExpenses’)), items(‘Apply_to_each’)[‘Cost_x0028_USD_x0029_’])

here we keep adding the cost to the TotalWeeklyExpenses variable and rewrites the updated sum to the same TotalWeeklyExpenses using the set variable action.

Graphical user interface, text, application

Description automatically generated

Thus, once the loop is completed, the total value will be present in the variable – TotalWeeklyExpenses.

Graphical user interface, text, application, Teams

Description automatically generated

This value is sent back to the parent flow. Thus, the overall child flow will look like:

Before moving on to Parent flow creation, we can manually test the flow by giving the list name to see if it is returning the total cost of items that were procured this week.

Graphical user interface, application

Description automatically generated

We can see that the child flow has run successfully and it has summed up the cost of all the procurement records of the current week

Graphical user interface, application, Teams

Description automatically generated

Parent Flow Development

Now let’s head over to the same solution, and create a new cloud flow of type Scheduled so that it will run on a specific schedule every week.

Graphical user interface, application

Description automatically generated

We will schedule it to run every Friday of the week

Graphical user interface, text, application, email

Description automatically generated

We will also add two array variables:

  • ListNames : To hold the lists over which the child flow should do the cost calculation processing
  • DepartmentCost : To hold the total costs returned by the child flow
Graphical user interface, text, application, email

Description automatically generated

Now we will add an apply to each and loop over the ListNames array and add the action to run the child flow. ListName parameter in the Run a child flow action is the input parameter we had defined inside the child flow. We will pass over the current iteration array value which would be Marketing Procurement List in the first iteration followed by Travel Procurement List in the second iteration.

Graphical user interface, text, application, email

Description automatically generated

The total cost processing happens within the child flow which is returned as an output parameter in the name totalcost. We will append it to the DepartmentCost array so that in Index 0 it will contain the Marketing Departments’ total cost and Index 1 will have Travel Department’s total cost.

Let’s get the corresponding values using the compose action

  • Marketing Total Cost : string(variables(‘DepartmentCost’)[0])
  • Travel Total Cost : string(variables(‘DepartmentCost’)[1])

Finally, we will send out the weekly status mail by adding the total cost derived from the above compose actions in the respective Marketing and Travel placeholders in the mail.

Graphical user interface, text, application, email

Description automatically generated

The overall Parent flow will look like this:

Graphical user interface, application

Description automatically generated

Testing the flow

Thus, we have completed the Parent and child flow, let’s do end-to-end testing. As the parent flow is set to run on a schedule, for testing, we can invoke it manually. The parent flow has run successfully and has invoked the child flow twice within the apply to each

Graphical user interface, application, Teams

Description automatically generated

It has also successfully returned the calculated total cost for both departments

Graphical user interface, application, email

Description automatically generated

And we have received the weekly mail as well:

Summary

Thus, we saw how to implement a Parent and Child flow in a solution to implement a weekly calculation which eventually sends out the scheduled mailer to the stakeholders.

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles