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

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

How to break out of ForAll loop based on a If condition?

(0) ShareShare
ReportReport
Posted on by

Hi All,

 

I wanted to understand how to break out of ForAll loop in powerapps based on a If condition.

 

Below is the code used for my ForAll loop. According to the below code, I am setting variable dupleave to false initially and the moment it turns to true inside ForAll, it should break out of both the loops.

 

-----------------------------------------------------

 

UpdateContext({dupleav: false});

ForAll(ourCollection, (ForAll(ourCollection1, If(LeaveDates = AppliedDates, dupleav = true, dupleav = false))))

 

-----------------------------------------------------

 

Thanks,

 

Vishwas.

I have the same question (0)
  • SkiDK Profile Picture
    1,016 on at
    Re: How to break out of ForAll loop based on a If condition?

    Hi @Anonymous 

    There's no way to break a loop. You also cannot use Set or UpdateContext in a forall. Your code won't work in PowerApps.

    Maybe there is a different way to solve what you want to do. Could you give is more information about that? In many cases a forall is not necessary at all.

    An alternative to breaking a loop is using a condition based on a column value of the current record.

    ForAll(collection, If(field1 = true, action1, action2))

    It won't break the loop, but it could change the behavior if field1 = false. You could also do nothing at that point, then the loop will be over fast since it has no code to run. I think this is the closest you can get with breaking a loop in PowerApps.

    I hope this helps. If you share more information with your case, we might be able to tackle it another way.  

  • timl Profile Picture
    35,811 Super User 2025 Season 2 on at
    Re: How to break out of ForAll loop based on a If condition?

    Vishwas,

    I agree with @SkiDK 

    It's preferable to find a different way to do. The documentation makes mention of how there are more efficient alternatives.

    https://docs.microsoft.com/en-gb/powerapps/maker/canvas-apps/functions/function-forall 

    If what you want to query is - are there any records in ourCollection1 where the LeaveDates field matches the AppliedDates field, you could count the rows that match this condition and if it's greater than 0, that would provide you with the result.

    The formula would look something like this.

    UpdateContext(
    {dupleav: (CountIf(ourCollection1, LeaveDates=AppliedDates ) > 0}
    )
  • Community Power Platform Member Profile Picture
    on at
    Re: How to break out of ForAll loop based on a If condition?

    Hi @SkiDK ,

     

    Thanks for your reply.

     

    Here is what I need to achieve.

     

    1. I am storing all dates from a table (VacationMaster) which contains all leaves applied by an employee i.e. ourCollection1

     

    2. I am storing all dates that are between from and to date selected from date picker on leave application form i.e. ourColleciton

     

    3. Now I am trying to check if each date in ourColletion exists in ourCollection1 i.e. each LeaveDates of ourColletion is equal to each AppliedDates of ourColletion1 and set value of variable dupleav to true (but as explained variable value cannot be set in ForAll)

     

    4. At this point, if dupleav is true for any 1 of the cases then display a message saying "Leave already applied"

     

    Hope this explains what I am looking to achieve.

     

    I have pasted the code below for reference:-

     

    -------------------------------------------------

     

    Collect(ourCollection, {LeaveDates: ""});

    Clear(ourCollection);

    ForAll(
    AddColumns(
    FirstN(
    [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
    DateDiff(FrmDt.SelectedDate, ToDt.SelectedDate, Days) + 1),
    "Day",
    "Day " & (Value + 1),
    "Date1",
    DateAdd(FrmDt.SelectedDate, Value, Days)
    ),
    Patch(
    ourCollection,
    Defaults(ourCollection),
    {
    LeaveDates: Date1
    }
    )
    );
    Collect(ourCollection1, {AppliedDates: ""});
    Clear(ourCollection1);
    ForAll(Filter(VacationMaster, Requester = EmpList.Selected.Result),
    ForAll(
    AddColumns(
    FirstN(
    [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
    DateDiff(DateValue(FromDate), DateValue(ToDate), Days) + 1),
    "Day",
    "Day " & (Value + 1),
    "Date2",
    DateAdd(DateValue(FromDate), Value, Days)
    ),
    Patch(
    ourCollection1,
    Defaults(ourCollection1),
    {
    AppliedDates: Date2
    }
    )
    ));
    UpdateContext({dupleav: false});

    ForAll(ourCollection1, (ForAll(ourCollection, If(ThisRecord.LeaveDates = AppliedDates, dupleav = true, dupleav = true))));

     

    -------------------------------------------------

     

    Thanks,

     

    Vishwas.

  • Community Power Platform Member Profile Picture
    on at
    Re: How to break out of ForAll loop based on a If condition?

    Hi All,

     

    Since ForAll does not allow to set value for any variable, I am using a temporary collection to 'sampcol' to set value of a variable 'var1' to 10 if LeaveDates = AppliedDates and 20 if it does not match for each record. 

     

    After this I am checking count of 'sampcol' collection with number of reocrds with value of 10 for var1.

    If count of sampcol (with records containing value of 10) is greater than 0 then set dupleav = true and also disable 'ApplyButton' else set dupleav = false and enable 'ApplyButton'. But when count of sampcol is greater than 0 'ApplyButton' is not getting disabled.

     

    I have written the code in 'OnChange' trigger of date picker (toDt).

     

    Please help me understand why is the 'ApplyButton' not getting disabled even when count of sampcol collection is greater than 0.

     

    ---------------------------------------------------------------------------------------

     

    UpdateContext({dupleav: false});

    Clear(sampcol);

    ForAll(ourCollection1, (ForAll(ourCollection, If(ThisRecord.LeaveDates = AppliedDates, Collect(sampcol, {var1: 10}), Collect(sampcol, {var1: 20})))));

    If(Count(Filter(sampcol, var1 = 10)) > 0, dupleav = true && ApplyButton.DisplayMode.Disabled, dupleav = false && ApplyButton.DisplayMode.Edit)

     

    ---------------------------------------------------------------------------------------

     

    Thanks,

     

    Vishwas.

  • Verified answer
    Community Power Platform Member Profile Picture
    on at
    Re: How to break out of ForAll loop based on a If condition?

    Hi Again,

     

    Finally I was able to achieve what I wanted to.

     

    The button was disabled during OnSelect event of date picker by setting the value of global variable to displaymode (edit or disabled based on condition). Now, assigned the global variable to displaymode of the button and it is working as expected.

     

    Thanks for all the help and inputs!!

     

    Vishwas.

  • suyash_1990 Profile Picture
    2 on at
    Re: How to break out of ForAll loop based on a If condition?

    Hello, lets say a for all loop is working for 10 iterations, after 5 iteration my objective is achieved now I want to break out of for all loop. How to do it in Power apps

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

Coming soon: forum hierarchy changes

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

Chiara Carbone – Community Spotlight

We are honored to recognize Chiara Carbone as our Community Spotlight for November…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 651 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 385 Super User 2025 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 230 Super User 2025 Season 2

Last 30 days Overall leaderboard