cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Shalom47
Helper I
Helper I

Add Checkbox values to collection if changed from their default values

Hi Thank you for taking your time to read this.

I am trying to add the check box values to a collection as below:

 

I have a screen with gallery with many checkboxes lets say Chk1,Chk2,Chk3 etc., whose text values are Checkbox1,Checkbox2,Checkbox3 so on., which have default values fetched from a SQL data source.

 

I have two collections named ColRemoveItem and ColAddItem. Now I want to add the text value of the checkboxes into collection ColRemoveItem if the user changes the checkbox value from default value(if it is true) and make it to false ,in the same way I want to add the text value of the checkbox into collection ColAddItem if the user changes the checkbox value from default value(if it is false) and make it to true.

 

And also I want to remove the item from the respective collection if the user makes the checkbox values back to default value.

 

I have tried assigning the default checkboxes values to some variables and checking the user input for checkboxes with variables but it has become hectic as there are many checkboxes and many variables are needed. Could you please help me to provide the best solution for this problem?

1 ACCEPTED SOLUTION

Accepted Solutions

Hey @Shalom47 ,

 

Before adding item colAddItem and colRemoveItem, I changed the code to check the SQL datasource for default values.

Oncheck 

If(CountRows(Filter(SQLdatasource,Key=ThisItem.Key && flag = true)) <=0,Collect(ColAddItem,ThisItem.Key));RemoveIf(ColRemoveItem,Value = ThisItem.Key)

 OnUncheck 

If(CountRows(Filter(SQLdatasource,Key=ThisItem.Key && flag = false)) <=0,Collect(ColRemoveItem,ThisItem.Key));RemoveIf(ColAddItem,Value = ThisItem.Key);

 

Where key is the checkbox value and flag hold the Boolean value.

View solution in original post

4 REPLIES 4
Ashwin7104
Super User
Super User

Hey @Shalom47 ,

 

I assume the checkboxes are loaded in gallery from a SQL data source.  I initialized two collections like below in Onscreen visible property to make sure to reset

ClearCollect(ColAddItem,Blank()); ClearCollect(ColRemoveItem,Blank());

 Then use below code in Oncheck property of checkbox

 Collect(ColAddItem,ThisItem.Value);
 RemoveIf(ColRemoveItem,Value = ThisItem.Value);

Similarly, below code in OnUncheck property of checkbox

RemoveIf(ColAddItem,Value = ThisItem.Value);
Collect(ColRemoveItem,ThisItem.Value)

 

Finally, colAddItem will hold all checked values and colRemoveItem will hold all unchecked items after screen loads with its defualt checkboxes values.

Shalom47
Helper I
Helper I

Hi @Ashwin7104 

 

Thanks for your reply,

 

I have tried the suggested method but it is also taking the default values into collection. For example: If checbox1 is checked by default and the user unchecks and checks it again(now the checkbox value is equal to the default value) still it is being loaded into collection. But  I don't want the default values of checkboxes to be loaded into the collection. The checkbox values must be captured only when they are changed from their default values and removed from the respective collection if the user changes them back to their default values

Hey @Shalom47 ,

 

Before adding item colAddItem and colRemoveItem, I changed the code to check the SQL datasource for default values.

Oncheck 

If(CountRows(Filter(SQLdatasource,Key=ThisItem.Key && flag = true)) <=0,Collect(ColAddItem,ThisItem.Key));RemoveIf(ColRemoveItem,Value = ThisItem.Key)

 OnUncheck 

If(CountRows(Filter(SQLdatasource,Key=ThisItem.Key && flag = false)) <=0,Collect(ColRemoveItem,ThisItem.Key));RemoveIf(ColAddItem,Value = ThisItem.Key);

 

Where key is the checkbox value and flag hold the Boolean value.

Shalom47
Helper I
Helper I

Thank you very much @Ashwin7104 , It is working perfectly😀

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Users online (3,147)