The ClearCollect function in Power Apps is a combination of the Clear and Collect functions. It first removes all records from a specified collection, and then adds a set of records to that same collection. This function is particularly useful for refreshing a collection with new data.
PowerApps – ClearCollect() function Syntax
ClearCollect(CollectionName, Items)
- CollectionName: The name of the collection you want to clear and then populate with new items.
- Items: The items or data you want to add to the collection after it has been cleared. This can be a list of records, a table, or any data source.
Notes:
- Efficient Data Loading: Use ClearCollect to efficiently clear and repopulate collections in one step, reducing the need for multiple function calls.
- Testing: Test the data being loaded into the collection to ensure it meets your requirements, especially when using filters or complex data sources.
- Performance Considerations: Be mindful of the size of the data being loaded into collections, as large datasets can impact app performance. Use delegation where possible to handle large data sources efficiently.
PowerApps - Clear and Populate a Collection with Simple Data
Suppose you want to clear a collection named Numbers and then populate it with a new set of numbers.
- Clear and Populate the Collection:
- Add a Button control.
- Set the OnSelect property of the Button to:
ClearCollect(Numbers, [1, 2, 3, 4, 5])
When you press the button, Numbers will be cleared and then populated with the values 1, 2, 3, 4, and 5.
PowerApps - Clear and Populate a Collection with Records
Suppose you have a collection named Products that you want to clear and then populate with new product records.
- Clear and Populate the Collection:
- Add a Button control.
- Set the OnSelect property of the Button to:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500},
{ProductID: 3, ProductName: "Monitor", Price: 300}
)
)
When you press the button, Products will be cleared and then populated with the new records.
PowerApps – Use ClearCollect() function with Data from a Data Source
Suppose you want to refresh a collection named Tasks with data from a SharePoint list named TaskList.
- Connect to the SharePoint List:
- In Power Apps, go to the Data tab.
- Add a data source and connect to your SharePoint site and list (TaskList).
- Clear and Populate the Collection:
- Add a Button control.
- Set the OnSelect property of the Button to:
ClearCollect(Tasks, TaskList)
When you press the button, Tasks will be cleared and then populated with the data from the TaskList SharePoint list.
PowerApps - Combine ClearCollect() function with Filtering
You can use ClearCollect to clear and then populate a collection with filtered data.
- Connect to the Data Source:
- Assume you have a SharePoint list named Employees with fields Name and Department.
- Clear and Populate the Collection with Filtered Data:
- Add a Button control.
- Set the OnSelect property of the Button to:
ClearCollect(FilteredEmployees, Filter(Employees, Department = "IT"))
When you press the button, FilteredEmployees will be cleared and then populated with employees from the IT department.