Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Using Flows
Answered

How to exist from each loop

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

 

Is there way to exist from apply to each loop?

 

Thanks,

Upendra Singh

  • Pstork1 Profile Picture
    65,997 Most Valuable Professional on at
    Re: How to exist from each loop

    Here's how to change your loop to use a Do Until Loop, which you can break out of.

    1) Initialize two variables, one to hold the number of items returned by the Get Items and the other as a Boolean set to False.

    2) Use a DoUntil Loop to iterate through the items returned by Get Items. When you want to break out of the Loop set the Boolean variable to True.  The loop will terminate on the next iteration.

    3) Access the values of the items inside the loop using iterationIndex as an index on the collection.  For example this JSON returns the Title property of one the current Get Items list item.

    body('Get_items')?['value'][iterationIndexes('Do_until')]?['Title']

     

  • Verified answer
    v-alzhan-msft Profile Picture
    on at
    Re: How to exist from each loop

    Hi @v-upsi ,

     

    If you don't know the place the item you needed, I'm afraid that your requirement couldn't be achieved in Microsoft flow currently.

     

    Best Regards,

    Alice

     

    Community Support Team _ Alice Zhang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • v-upsi Profile Picture
    Microsoft Employee on at
    Re: How to exist from each loop

    Hi @efialttes 

     

    It have multiple condition check inside my apply to each.

     

    Thanks,

    Upendra Singh

  • efialttes Profile Picture
    14,756 on at
    Re: How to exist from each loop

    @v-upsi 

    Does your 'Apply to each' contain only the Condition action block? If so, you can probably replace both 'Apply to each' and 'Condition', add 'Filter array' action block instead

    Hope this helps

  • efialttes Profile Picture
    14,756 on at
    Re: How to exist from each loop

    @Pstork1 

    You are right, current iteration will fail, but the loop will start next iteration. That's why I call the suggested workaround as 'partial'. 

    Hope this helps

  • Pstork1 Profile Picture
    65,997 Most Valuable Professional on at
    Re: How to exist from each loop

    In an apply to each loop failing an individual iteration will make the loop go on to the next iteration of the loop.  As I said if you want to be able to break out of a loop you'll need to use a do until loop set to exit after either setting a control variable to a certain value or iterating through the last item.

  • v-upsi Profile Picture
    Microsoft Employee on at
    Re: How to exist from each loop

    Hi @v-alzhan-msft 

     

    I just explain as example because if it's apply to each so I don't know when this condition will match, it might match at first iteration, second and so on. So when that condition match then it will exist from each loop.

     

    Thanks

    Upendra Singh

  • v-alzhan-msft Profile Picture
    on at
    Re: How to exist from each loop

    Hi @v-upsi ,

     

    If you just want to check the first item in the array, you could create the flow as below:

    1.png

    body('Get_items')?['value']?['0']
    outputs('Compose_3')?['Title']

     

    Best Regards,

    Alice

     

    Community Support Team _ Alice Zhang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • v-upsi Profile Picture
    Microsoft Employee on at
    Re: How to exist from each loop

    Hi @Pstork1 

     

    Thanks for your suggestion on this. I just want to know if add some logic inside the apply to each condition to fail the steps so what will happen if i have other step next to apply to each, it will also fail to run which i don't want.

    I have other step which use this apply to each condition value and perform some other action.

     

    Thanks,

    Upendra Singh

  • v-upsi Profile Picture
    Microsoft Employee on at
    Re: How to exist from each loop

    Hi @v-alzhan-msft 

     

    I have apply to each condition where i have added some condition to validate so what i want is while iterating the loop if condition meet at first iteration so my apply to each condition should exist from first iteration and it should not iterated remaining items from loop. And after apply to each there is other step which depend on condition value which should execute.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,645 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,997 Most Valuable Professional

Leaderboard

Featured topics

Restore a deleted flow