How to use ClearCollect in powerapp
The ClearCollect function in Power Apps is used to clear the contents of a collection and replace it with new data. In this explanation, we will provide a step-by-step guide on how to use the ClearCollect function effectively in Power Apps. We will explore its syntax, discuss the necessary steps, and provide example code to illustrate its usage.
Step 1: Create a Collection First, you need to create a collection to store data. A collection is an in-memory table that can hold multiple records with multiple fields. You can create a collection manually or populate it from a data source.
Step 2: Add a Control to Trigger the ClearCollect Function Next, add a button or any other control to your Power App’s screen that will trigger the ClearCollect function. This control will be used to initiate the clearing and collection of new data.
Step 3: Define the OnSelect Property of the Control Select the control you added in the previous step, and in the formula bar, enter the following code:
OnSelect = ClearCollect(CollectionName, NewData)
In this code, “CollectionName” represents the name of the collection you want to clear and populate with new data. Replace “NewData” with the data source or the formula that generates the new data you want to add to the collection.
Step 4: Test the ClearCollect Function Run your Power App, and click the control you added to trigger the ClearCollect function. The collection specified in the function will be cleared, and the new data will replace the previous contents.
Here’s an example scenario to illustrate the usage of the ClearCollect function:
Suppose you want to create a collection named “EmployeeData” that stores employee records from a SharePoint list.
To create and populate the “EmployeeData” collection, follow these steps:
Step 1: Create a Collection In the OnStart property of your app, enter the following code:
ClearCollect(EmployeeData, 'SharePointListName')
In this code, “EmployeeData” is the name of the collection you are creating, and ‘SharePointListName’ is the name of the SharePoint list you want to populate the collection with.
Step 2: Add a Button Control Add a button control to your Power App’s screen that will trigger the ClearCollect function.
Step 3: Define the OnSelect Property of the Button Control Select the button control, and in the formula bar, enter the following code:
OnSelect = ClearCollect(EmployeeData, 'SharePointListName')
This code clears the “EmployeeData” collection and populates it with the latest data from the SharePoint list.
Step 4: Test the ClearCollect Function Run your Power App, and click the button control you added. The “EmployeeData” collection will be cleared and updated with the current data from the SharePoint list.
The ClearCollect function is a powerful tool in Power Apps to clear the contents of a collection and replace it with new data. By incorporating this function into your Power Apps, you can dynamically update collections with the latest data from data sources or generate new data based on specific conditions.