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 / Odd behavior when remo...
Power Apps
Answered

Odd behavior when removing item from collection

(0) ShareShare
ReportReport
Posted on by 10

Hi all,

 

I've encountered a rather odd (and inconsistent) issue when removing items from a collection.

 

Here's my scenario:

  • I have a gallery connected to a SharePoint list with about ~3000 items. The gallery items can be filtered by selecting some preset values or by using a text input field to search.
  • Each gallery item has a checkbox. When the checkbox is selected, the item will be added to a collection. The contents of the collection are displayed in a second gallery on the right side of the screen.
  • To prevent users from having to select numerous records manually, I have a checkbox outside the gallery that will enable users to select all items currently shown in the gallery. When the checkbox is unchecked, all items currently displayed in the gallery should be removed from the collection.

 

Often this works as intended, but occasionally the unchecking the check-all checkbox removes the wrong item from the collection.

 

Here I've got 5 records currently in my collection. I've just added record 2238 by checking the checkbox outside of the gallery.

 

I uncheck the checkbox, but the item currently shown in my gallery is still selected and is still part of the collection. The OnUncheck action has removed the first item of the collection (record 537) instead of record 2238.

 

I toggle the checkbox again--again the OnUncheck action has removed the first item in my collection (1648), but still hasn't gotten rid of the item shown in my gallery (2238).

 

Here's the checkbox's OnUncheck configuration:

 

 

ForAll(
 gallery_ItemSelection.AllItems,
 Remove(
 collection_CurrentlySelectedItems,
 LookUp(
 collection_CurrentlySelectedItems,
 ID = ThisRecord.ID
 )
 )
)

 

 

 

I'm finding this behavior very confusing, as I'm targeting the item in the collection by its unique ID. I've also tried a few variations on looking up the item from the collection (LookUp, Filter(First()))--but the problem persists. It seems like the ForAll function is caching the IDs of the previously selected records even though they are no longer displayed in the gallery.

 

Has anyone seen anything like this before? Thank you!

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @ghost5472 

    To start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

    It is more efficient to use the function as intended and will provide better performance.

     

    Please consider changing your Formula to the following:

    Remove(collection_CurrentlySelectedItems,
     ForAll(gallery_ItemSelection.AllItems As _item,
     LookUp(collection_CurrentlySelectedItems, ID = _item.ID)
     )
    )

     

    I hope this is helpful for you.

  • WarrenBelz Profile Picture
    154,930 Most Valuable Professional on at

    Hi @ghost5472 ,

    Try this attack from another direction

    With(
     {
     wSelected:
     Filter(
     gallery_ItemSelection.AllItems,
     YourCheckBoxName.Value
     )
     },
     ForAll(
     wSelected As aRemove,
     RemoveIf(
     collection_CurrentlySelectedItems,
     ID = aRemove.ID
     )
     )
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    Visit my blog Practical Power Apps

  • ghost5472 Profile Picture
    10 on at

    You're absolutely right, I have been conceptualizing ForAll like a for loop--your solution makes sense and worked like a charm. Thank you!!

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @ghost5472 

    Yes - the ForAll function is one of the most powerful functions in PowerApps - when used properly!  If it is used as a For-Loop, then it becomes just a so-so function as it is not its intended purpose.

    Most all of the data oriented functions in PowerApps accept a Table as a parameter for the changes/removals/etc to perform.  This is what ForAll was made for...to create that Table.

     

    Glad that it helped!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 551

#2
WarrenBelz Profile Picture

WarrenBelz 430 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 298

Last 30 days Overall leaderboard