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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Potential Bug: Cannot ...
Power Apps
Answered

Potential Bug: Cannot have a switch inside of an If statement inside of a ForAll

(0) ShareShare
ReportReport
Posted on by 375

Hi,

 

It seems there is a bug that prevents you from having a collect function inside of switch statement inside of a if statement inside of a forall, but only if your if statement has an else condition...

 

This code gives an error:

 

 

ForAll(galInsertedControls.AllItems, If(true, Switch(true, true, Collect(colTempJSON, true)), true));

 

 

 

This code doesn't:

 

 

ForAll(galInsertedControls.AllItems, If(true, Switch(true, true, Collect(colTempJSON, true))));

 

 

 

The only difference is I don't have an else section on my If statement.

You can swap the if and switch statement so that the if statement is inside of the switch instead but there is not difference to the result.

I just tried this with just nested if statements instead of a switch and appear to get the same result...

 

Is this a bug?

Categories:
I have the same question (0)
  • TheRobRush Profile Picture
    11,128 Moderator on at

    collect, patch, etc should really go on the parent level of a forall anyways, way more resource intensive to put it inside

     

    What are you referencing as true? the way you have it written it will be true every time, because its just goign to say is there a gallery item here, and sicne the reference is the gallery.allitems every single result will have a gallery item and thus be true.

  • TheRobRush Profile Picture
    11,128 Moderator on at

    what you want to do is something like

    ClearCollect(colTempJSON, 
    
    ForAll(galInsertedControls.AllItems As firstPass, If(firstPass.ControlName.DataType = true 0r "true" or "String" or Value etc, true, false)))
  • BradonK Profile Picture
    375 on at

    Hi @TheRobRush,

     

    The formula I put in my original post was just what I had when I was testing what exactly was happening.

     

    My actual formula was meant to look something like this but would have multiple conditions in the switch statements though:

    ForAll(galInsertedControls.AllItems, If(ThisRecord.ID in colTempJSON.id, Switch(ThisRecord.Control, "Text Input", Patch(colTempJSON, LookUp(id = ThisRecord.ID), {label: "test"})), Switch(ThisRecord.Control, "Text Input", Collect(colTempJSON, Table({control: "test"})))));

     

    I'm not sure how I could make your example work, as I need to simply patch the data not collect it if there is already a record in the collection.

     

    Thanks

  • TheRobRush Profile Picture
    11,128 Moderator on at

    Just left the office so if no one gets to this before tomorrow morning i will give you correct formula, you can still do it in correct order with those reqs, will just 

     

    Patch(

            Forall(

                    If(

                     Already exists {ID 

                    Doesnt exist {

     

     

    Basically when updating via patch just include the ID in the {} and when adding via patch dont include it, which we can code into the forall.

  • BradonK Profile Picture
    375 on at

    @TheRobRush,

     

    Will that add a new record if one doesn't already exist?

    I'm not sure how to use the formula you showed in the post above.

     

    Thanks

  • TheRobRush Profile Picture
    11,128 Moderator on at

    Yes, it will both update existing and add new, tho the example I gave you directly above is just very / bare bones this is the structure /

     

    I will give one closer to your specific case tomorrow

  • TheRobRush Profile Picture
    11,128 Moderator on at

    try this

    Patch(
     colTempJSON,
    		ForAll(galInsertedControls.AllItems As dataSet, 
    				If(dataSet.ID in colTempJSON.id,
     {ID: dataSet.ID,
     control: "Test"},
     {control:"Test"})))
  • Verified answer
    BradonK Profile Picture
    375 on at

    Hi @TheRobRush,

     

    Thanks for the help.

    However, I found a way around this issue which also makes my formula much simpler.

    Instead of collecting the data through a for all, all at once, I collect it as it is entered.

    I have added a collect/patch formula in the OnChange/OnSelect properties of my controls that updates the collection as the data is changed.

    I'm aware it's probably not as performant, but it's for a small set of controls and won't cause any issues.

     

    Really appreciate the effort though.

     

    Thanks

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 358 Most Valuable Professional

#2
11manish Profile Picture

11manish 214 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 185

Last 30 days Overall leaderboard