The Power Apps collection is a temporary storage area for data. Sometimes, we need to update or add a new record to the collection. Follow this tutorial to learn more about the Power Apps update collection. Such as:
- Update Collection in Power Apps
- How to Add Items to Collection in Power Apps
Update Collection PowerApps
Updating a Power Apps collection typically involves adding, modifying, or removing records from the collection based on user actions or other events within the application.
Suppose you want to update the collection item in Power Apps; you can use two different functions. Such as:
- Update: The Power Apps Update Function is used to replace the entire record in a data source
- UpdateIf: The Power Apps UpdateIf Function is used to modify one single or more values in one or more records that match one or more conditions
1. PowerApps Update Function Syntax:
Update( DataSource, OldRecord, NewRecord [, All ] )
Where,
- DataSource = This contains all the records that you want to replace. It may be a Table, Collections, etc
- OldRecord = It is the record that you want to replace
- NewRecord = It is the replacement record. This is not a change record. Here, the entire record is replaced
- All = Mention the All argument to remove all copies of the record
2. PowerApps UpdateIf Function Syntax:
UpdateIf( DataSource, Condition1, ChangeRecord1 [, Condition2, ChangeRecord2, ... ] )
Where,
- DataSource = This data source contains one or more records you want to modify
- Condition(s) = In this condition, you can use column names of DataSource in the formula. It evaluates to True for the record that you want to modify
- ChangeRecord(s) = You can provide a change record of new property values that satisfy the condition
Power Apps Update Collection [Using Update Function]
I have created a Power Apps collection named “colProducts” from my SharePoint list [Product Details]. This list contains the below fields.
Column Name | Data Type |
Product Name | It is a default single line of text |
Manufacturer | Choice |
Price | Currency |
Quantity | Number |
Purchase Date | Date and time |
In Power Apps, there is a Gallery control retrieving all the collection records, as shown below
Now, I would like to update the Title, Price, and Quantity based on the Title field [Product Name]. The output is shown in the screenshot below.
To do so, follow the below steps. Such as:
1. To Create a Power Apps collection using a SharePoint list, select the App object and set its OnStart property to the code below.
OnStart = ClearCollect(colProducts, 'Product Details')
Where,
- colProducts = Power Apps collection
- ‘Product Details’ = SharePoint Online list
2. On the Power Apps Screen -> Insert a Gallery control and set its Items property to the code below.
Items = colProducts
3. Finally, insert a Button control [Update Collection] and set its OnSelect property as:
OnSelect = Update(
colProducts,
First(
Filter(
colProducts,
Title = "Laptop"
)
),
{
Title: "Apple MacBook Pro 14",
Price: 1500,
Quantity: 4
}
)
Where,
- Title, Price, Quantity = Power Apps collection fields
4. When the user selects or taps the button control, the specific item records will be changed in the Power Apps collection, as shown below.
Power Apps Update Collection Item [Using UpdateIf Function]
In this example, whenever the user selects any collection record from the gallery control, it will navigate to the edit form, where you can edit or update the selected collection record.
Once your updates are done in the edit form, click on the button control to save the updated records in the collection as well as redirect to the gallery control, as shown below.
Output:
To work around this, follow the steps below. Such as:
1. Insert another blank Screen [Product Details Edit Screen] -> Add an Edit form and set its DataSource and Item properties using the below codes.
DataSource = 'Product Details'
Item = gal_Records.Selected
Where,
- gal_Records.Selected = Power Apps gallery control selected value [Collection Record]
2. Now, go to the Product Details Screen -> Select the gallery control -> Click on the NextArrow icon and set its OnSelect property as:
OnSelect = Navigate(
'Product Details Edit Screen',
ScreenTransition.None
)
Where,
- ‘Product Details Edit Screen’ = Power Apps 2nd screen
3. Finally, on the Power Apps second Screen -> Insert a Button control [Update] and set its OnSelect property to the code below.
OnSelect = UpdateIf(
colProducts,
Title = gal_Records.Selected.Title,
{
Title: txt_Product_Name.Text,
Price: txt_Product_Price.Text,
Quantity: txt_Product_Quantity.Text
}
);
Navigate(
'Product Details Screen',
ScreenTransition.None
Where,
- txt_Product_Name, txt_Product_Price, txt_Product_Quantity = Power Apps edit form data card value names
- ‘Product Details Screen’ = Power Apps 1st screen name
4. Save, Publish, and Preview the app. Once the user selects any collection record from the gallery control, it will navigate to the edit form, where you can edit or update the selected collection record.
5. Once your updates are done, click on the button control to save the updated records in the collection, as well as redirect to the gallery control, as in the screenshot below.
This way, we can update an item in the Power Apps
How to Add Items to Collection in Power Apps
To add a new single record or multiple records to the Power Apps Collection, follow the below-mentioned steps. Such as:
1. On the Power Apps app Screen -> Add four Text Labels and rename all the label names by double-clicking on them, as in the screenshot below.
2. In the same way, add four Text input controls for four Labels and set their Default properties are blank.
3. Now, add a Button Control and rename the button Text as Add item To Collection, as shown below.
4. Now, select the Button control and set its OnSelect property to the code below.
OnSelect = Collect(
colCandidateInfo,
{
CandidateID: txt_CandidateID.Text,
CandidateName: txt_CandidateName.Text,
PhoneNumber: txt_PhoneNumber.Text,
Address: txt_Address.Text
}
)
Where,
- colCandidateInfo = Power Apps collection name
- txt_CandidateID, txt_CandidateName, etc. = Text input control names
5. Once your app is ready, Save, Publish, and Preview the app. Enter the Candidate ID, Candidate Name, Phone Number, and Address in the text input fields. Click on the Add Item To Collection button.
6. Once you click or tap the button control, the new item will be added to the collection. To view the collection, click on the Variables section (X), expand the Collections dropdown, and Select the View Table option.
Have a look at the below screenshot for the output.
This way, you can work with the Power Apps to add value to the collection.
Also, you may like:
- Remove a Column from Power Apps Collection
- Power Apps Collection GroupBy
- Patch Power Apps Collection to SharePoint List
- Power Apps Length Of String
I trust this Power Apps tutorial is useful. If you have any requirements related to the Power Apps update collection, you can follow the above different approaches to do it easily.
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
Hi All
My request is how to call column in collection based on status(Available, Not Available) the color can display button Red, Green. in another screen. please help me any one .
I am struggling with my collection that contain a column for subtotals and I need to generate aggregation sum of rows in that column to give Total amount for my shop list . How can I solve this please?
This Sum(DataTable4.Selected.SUBTOTAL) returns just value of template cell “first record”
Hi Bijay, thank you so so much for the detailed post. I however have a question. I used the UpdateIf to update a url in one of my collection but the changes are not saved when i open a new instance of the same power app. Is the way to save the changes different from regular file—->save?
Is it possible to create collections dynamically?
I am reading data from different files and showing that data to the user. Each file has different headers, and I was wondering if there was a way to dynamically create collections each time I read a file.