Latest Posts

PowerApps ClearCollect function explained with examples


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:

  1. Efficient Data Loading: Use ClearCollect to efficiently clear and repopulate collections in one step, reducing the need for multiple function calls.
  2. Testing: Test the data being loaded into the collection to ensure it meets your requirements, especially when using filters or complex data sources.
  3. 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.

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

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

  1. 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).
  2. 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.

  1. Connect to the Data Source:
    • Assume you have a SharePoint list named Employees with fields Name and Department.
  2. 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.


We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint