cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
guyinazo
Level 8

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

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.

guyinazo
Level 8

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

Super User
Super User

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

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

guyinazo
Level 8

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

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. 

guyinazo
Level 8

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.

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

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
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (5,441)