cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User
Super User

Re: Method to Remove items from local Collection

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
Highlighted
Super User III
Super User III

Re: Method to Remove items from local Collection

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.

Highlighted
Advocate V
Advocate V

Re: Method to Remove items from local Collection

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

Highlighted
Super User III
Super User III

Re: Method to Remove items from local Collection

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'.

Highlighted
Super User
Super User

Re: Method to Remove items from local Collection

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

Highlighted
Advocate V
Advocate V

Re: Method to Remove items from local Collection

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

Highlighted
Super User
Super User

Re: Method to Remove items from local Collection

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

Highlighted
Advocate V
Advocate V

Re: Method to Remove items from local Collection

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.

Highlighted
Super User
Super User

Re: Method to Remove items from local Collection

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>})

Highlighted
Super User
Super User

Re: Method to Remove items from local Collection

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
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (7,865)