web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to exist from each...
Power Automate
Unanswered

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

Categories:
I have the same question (0)
  • Pstork1 Profile Picture
    68,697 Most Valuable Professional on at

    'There is no way to exit early from a foreach Loop. You'll need to redesign the loop to use a do until loop to loop through each item using an index. Using a variable you can then control when to exit the do until loop.

  • efialttes Profile Picture
    14,756 on at

    Hi!

    There is a partial workaround, that is to force a crash by means of a wrong WDL expression, something like:

     

    split('qwerty','a')[2]

     

    The point is, current iteration will fail, but the loop will start next. That's why I call it 'partial'. The point is, overall 'Apply to each' execution will be marked as 'Failed'. 

    If this approach is anyway fine for you, then you need to be careful when designing next steps after 'Apply to each', so you need to implement a branch whose 'Run after' settings shall take care of the error and apply the expected logic

     

    Hope this helps

  • Pstork1 Profile Picture
    68,697 Most Valuable Professional on at

    That will break you out of the current iteration, but won't break you out of the actual loop will it?  It will run the next item in line.  I think the questioner wants to actually break out of the Loop.

  • v-alzhan-msft Profile Picture
    on at

    Hi @v-upsi ,

     

    Could you please explain more details about the your issue so we could better understand you and provide the solution?

     

    Do you want to check if an item exists within an "Apply to each"? If yes, you could take a try to refer to screenshot below to create the flow:

    1.png2.png

     

    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

    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.

  • v-upsi Profile Picture
    Microsoft Employee on at

    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-alzhan-msft Profile Picture
    on at

    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

    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

  • Pstork1 Profile Picture
    68,697 Most Valuable Professional on at

    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.

  • efialttes Profile Picture
    14,756 on at

    @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

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard