Power Apps ClearCollect function deletes all the records from a collection and then adds a different set of records to the same collection.
Basically, this function is a combination of Clearย and thenย Collect function.ย This functionย returns the modified collection as a table.
As you can see, here we have a canvas app which store the employee’s data to collection and then show those data on Gallery.
If you do not know how to create a collection you can read this post.. [ Collection in Power Apps]
You can directly see the data stored in collection. Just click on Variables button and expand the Collections and you can see the collection name EmployeeDetails.
Click on Ellipses and select View Table to see the data stored in collection.
Using ClearCollect function
As we know, ClearCollect function is used to delete the data stored in collection and adds new records to the same collection.
We have already seen, there are two records in collection.ย Let’s take a look at the code written on OnSelect property of Add button.
Lets understand the above code, It takes Employee Name and Department name values from Text input controls and adds the records in collection on Add button click.
Note that, EmployeeDetails is the name of collection, and txt_empname and txt_dptname are the name of Text input controls.
Collect ( EmployeeDetails, { EmployeeName: txt_empname.Text, DepartmentName: txt_dptname.Text } )
Now, we add another button which uses the ClearCollect function, and delete all the existing records from collection while add new records inside the collection.
Go to the Inset tab, select Button control from dropdown list.
Now, set the Text property of button to ClearCollect.
After that, select the Onselect property of button and write below code.
ClearCollect ( EmployeeDetails, { EmployeeName: txt_empname.Text, DepartmentName: txt_dptname.Text } )
Let’s run the app to validate it. Click on Preview the app button.
Now, add new record in collection. Provide employee name and department name then click on ClearCollect button.
And you can see the gallery. It shows only newly added record. That means, all the existing records are deleted from collection while new record is added to the collection.
Lets go and check the Collection. You can see there is only one record which we have added recently.
Also Read..
Highlight selected item in gallery
Email validation in Power Apps
Sort Items in gallery Power Apps
Change Item color in gallery based on value