Skip to main content

Fixed: Power Automate Adds a Second For Each Loop When You Pick a Token

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Inside a For each loop, I picked the Approvers field of the loop's own array from the dynamic content list, and Power Automate's new designer wrapped my action in a second For each over the same array. The fix is to undo it and type an expression that reads the outer loop's current item: items('For_each_department')?['Approvers'].

What I Was Doing​

I was building Get Waiting Approvals, the flow behind the Waiting for Approval screen of the Overtime Timesheet app. It reads the Departments list, and a loop called For each department goes through the departments to find the ones the person running the flow approves.

Inside the loop, I added a Select action named Approver emails, to turn the department's Approvers column (a SharePoint Person column that allows several people) into a list of email addresses (How to Pick One Field from Each Item with Select in Power Automate). In its From box, I opened the dynamic content list and picked Approvers under Get the departments.

What I Expected​

Approver emails to stay where I added it, directly inside For each department, with From set to the current department's approvers.

What I Got​

The designer moved Approver emails into a new loop called For each, inside For each department. The From box showed Approvers, and the action said Invalid parameters only because its Map was still empty:

The Approver emails pane with From set to the token Approvers and an empty Map, and the flow canvas showing For each department containing a new For each loop, which contains Approver emails marked Invalid parameters

The new loop's own pane showed what it loops over: the token Get items, which is the whole output of the Get the departments action:

The For each pane with Select an output from previous steps set to the token Get items

What I Checked​

The code of the new loop. Its Code view made the problem plain:

The For each Code view: type foreach, foreach set to outputs of Get_the_departments body/value, and inside it Approver_emails, a Select with from set to item()?['Approvers'] and an empty select

Code view of the added loop
{
"type": "foreach",
"foreach": "@outputs('Get_the_departments')?['body/value']",
"actions": {
"Approver_emails": {
"type": "Select",
"inputs": {
"from": "@item()?['Approvers']",
"select": ""
}
}
}
}

The added loop goes through all the departments again, and inside it item() is the department of that inner loop. Every pass of For each department would have run Approver emails once for every department in the list.

Microsoft's documentation. The functions reference defines item() as the current item of the repeating action it's used in, and items('<loopName>') as the current item of a named loop, which is how an action reads an outer loop's item from anywhere inside it. I found no page that describes when the new designer adds a loop by itself.

Why It Happens​

Approvers under Get the departments is a field of each item in that action's output, which is an array, so the token stands for one department's approvers. The designer added a loop over that output to give the token a current department to read from. For each department already loops over the same array, but the token came from Get the departments, not from the loop. Microsoft's documentation doesn't describe this behaviour; the Code view above is what shows it.

The Fix​

  1. Undo the change. I selected Undo twice: once for the token, once for the loop the designer added. Approver emails was back directly inside For each department.

  2. Point From at the outer loop's current item with an expression. In From, I typed /, chose Insert expression, and entered:

    Approver emails → From
    items('For_each_department')?['Approvers']

    The loop's name goes in with underscores for spaces (How to Use the items() Function in Power Automate). In the editor, the first Add showed This expression has a problem, and the second added it (Fixed: "This expression has a problem" in Power Automate):

    The Approver emails pane with an empty From box, and the expression editor beside it containing items('For_each_department')?['Approvers'] with the cursor at the end

  3. Fill in the rest of the action. In Map, switched to text mode, I entered toLower(item()?['Email']). Inside Select, item() is the current approver, so it stays item().

After the Fix​

Approver emails sits directly inside For each department, with From showing the items(...) expression and Map the toLower(...) one, and no second loop:

The Approver emails pane with From set to the expression token items(...) and Map to toLower(...), and the canvas showing Approver emails directly inside For each department with no other loop

With the rest of the flow built, each approver's Waiting for Approval screen listed only the entries of the departments they approve (Part 9).

Common Mistakes​

MistakeWhat happensDo this instead
Picking a field of the outer action's output inside its loopThe designer adds a second loop over the same arrayUse items('<loop name>')?['Field']
Leaving the added loop in placeEvery pass repeats the work for every itemUndo both changes
Using item() for the outer loop inside a Selectitem() there is the Select's own current rowitems('<loop name>') for the outer loop
Spaces in the loop nameThe expression doesn't find the loopUnderscores: For_each_department

Troubleshooting​

SymptomLikely causeFix
A For each appeared you didn't addA token from the loop's source actionUndo, then an items() expression
The action runs many times per itemA second loop over the same arrayCheck Code view for a nested foreach
This expression has a problem on a valid expressionThe expression editorSelect Add again
items() says the loop doesn't existThe name doesn't matchCopy the name from the loop's Code view

FAQ​

What's the difference between item() and items()?​

item() is the current item of the repeating action the expression sits in: inside Select, its current row. items('<loop name>') is the current item of the loop you name, from anywhere inside that loop, which is how an action in a Select or an inner loop reaches the outer loop's item.

Is a loop inside a loop always wrong?​

No. A loop over a different array, such as each department's approvers, is a real nested loop. The problem here was a second loop over the same array.

Conclusion​

Picking a field of a loop's source action from the dynamic content list made the new designer add a second For each over the same array. Undo it, and read the outer loop's current item with items('For_each_department')?['Approvers'].

Additional Resources​