cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Advocate V
Advocate V

Method to Remove items from local Collection

I have an app with various medical questions that I want to flag and collect a condition everytime the user selects "Yes".  Here is an example of my code behind a toggle button:  If(ynSmoker.Value,Set(ReviewFlag,true);Collect(ReviewItems,"Smoker"),false)

 

So my question would be, what if that user selects Yes, but then reverts back to No?  I need to Remove that item from the collection.  Would it be as simple as "Remove(ReviewItems,"Smoker")?  I dont think it would be that easy I am guessing

 

Thanks,

 

Brad

1 ACCEPTED SOLUTION

Accepted Solutions
Community Champion
Community Champion

Hello @guyinazo

 

Here is the method I use to remove items from a collection. Hope this helps. 

 

  • Use a Gallery with an embedded Checkbox control
  • Checkbox OnCheck property: 
    • Collect(
           <collection_name>, 
          {<column_name_1>: ThisItem.<column_name_1>, 
           <column_name_2>: ThisItem.<column_name_2>}
                 )
  • Checkbox OnUncheck property
    • Remove(
           <collection_name>,
                First(
                     Filter(
                     <collection_name>,
                     <column_name_1>=ThisItem.<column_name_2>)
                        ),
                     All)

 

Hope this helps your scenario. 

View solution in original post

11 REPLIES 11
Super User III
Super User III

Hi Brad,

 

You're right, it's not that simple! The Remove function works well against connected data sources, but it doesn't work against local collections. I think this is a bug in PowerApps.

 

https://powerusers.microsoft.com/t5/General-Discussion/Method-to-Remove-items-from-local-Collection/...

 

If you have a unique id that identifies the record that you want to remove from the local collection, a more reliable way is to call the RemoveIf function.

Now I am trying to think logically how to do this.  This questionnaire has around 50 of these medical conditions and if any of them are selected to "Yes", the app needs to capture that and I was thinking a Collection would work just fine (selecting Yes also trips a flag too but that is easy to handle).  As I was working on this, I was thinking if the user selected the wrong toggle or said yes, then reverted back to No, how it would impact that collection. Perhaps I could do a massive IF statment on the Submit button?  Also, I did see that topic that you referred to and I need to revisit it to see if I missed something.

 

Thanks

I think the approach of adding your logic to your Submit button might be best. That way, you don't need to worry about the possibility of a user toggling an answer from 'yes' back to 'no'.

Community Champion
Community Champion

Hello @guyinazo

 

Here is the method I use to remove items from a collection. Hope this helps. 

 

  • Use a Gallery with an embedded Checkbox control
  • Checkbox OnCheck property: 
    • Collect(
           <collection_name>, 
          {<column_name_1>: ThisItem.<column_name_1>, 
           <column_name_2>: ThisItem.<column_name_2>}
                 )
  • Checkbox OnUncheck property
    • Remove(
           <collection_name>,
                First(
                     Filter(
                     <collection_name>,
                     <column_name_1>=ThisItem.<column_name_2>)
                        ),
                     All)

 

Hope this helps your scenario. 

View solution in original post

Thanks @ericonlinefor this.  I will try it, and also accepted as a solution for now.  But yes, it is a bit quirky but dont see why it wouldnt work.

 

Brad

Cool. The "First(..", "Filter(...", and "All" will likely get you what you need. 

So I am hoping something like this: If(ynSmoker.Value,Collect(ReviewItems,"Smoker"),Remove(ReviewItems,First(Filter(ReviewItems,Value="Smoker")))) <--This is in the OnSelect of the toggle

 

The only thing I was unsure of is the column name because there is only one column in this collection and it is not specified so looking at the example here: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/create-update-collection it is Value.

I think you'll need to look closer at your Collect syntax. The curly braces and such.

 

I believe its Collect(<collection_name>, {<collection_column_name>:<value>})

And don't forget the "All". That parses the entire collection and removes all instances of the item. If for some reason the User recorded the same value twice, this would get both of them. 

Helpful resources

Announcements
News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

Power Apps Community Call

Power Apps Community Call- January

Mark your calendars and join us for the next Power Apps Community Call on January 20th, 8a PST

PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

Top Solution Authors
Top Kudoed Authors
Users online (10,741)