Introduction
In PowerApps, the Collect and ClearCollect functions play pivotal roles in managing data within your applications. Knowing the difference between these two functions is really important for working with data effectively in your apps. It helps you manage information better and build apps that work smoothly.
Collect Function in Power Apps
The Collect function in PowerApps is utilized to add new records or sets of records to a data source. It functions akin to appending data to an existing dataset
Example
Collect(EmployeesProfile,
{
Name: "Ms Dhoni",
Designation : "Developer",
Age: 31,
ContactNumber : 1111111111
}
)
- Select Gallery from the Gallery section.
- Click on the gallery you've just added to select it.
- Under the Properties tab, find the "Items" property.
- Set the Items property to the name of your collection.
- Once you've completed these steps, the gallery will automatically display the items from your collection.
In this example, the Collect function adds a new item with the name "Ms Dhoni" and a age of 31 to the "EmployeesProfile" collection.
ClearCollect Function in Power Apps
The ClearCollect function serves the purpose of replacing the entire contents of a collection with new data. It clears out any existing data within the collection before appending the new data provided. This function is akin to refreshing or updating the contents of a dataset.
Example
ClearCollect(
UserProfile,
Filter(
'Employee Details',
Designation.Value = "Developer"
)
);
Here
- UserProfile: Name of the collection
- Employees Details: Data source to filter
- Deisgnation Value: Filter Condition
To achieve the ClearCollect data follow the below steps.
- Select Gallery from the Gallery section.
- Click on the gallery you've just added to select it.
- Under the Properties tab, find the "Items" property.
- Set the Items property to the name of your collection.
The ClearCollect function clears the "UserProfile" collection and then fills it with all products from the "EmployeeDetails" data source where the category is "Developers".
Conclusion
So Both Collect and ClearCollect in PowerApps help work with data, but they each have a different job. Collect adds new data to what's already there, like putting more items in a basket. ClearCollect, on the other hand, clears out everything and then adds new data, like replacing all the items in the basket with new ones.