How to Patch a Collection in Power Apps

This Power Apps tutorial will go over how to patch the Power Apps collection based on various scenarios.

We recently received a request to patch a collection within Power Apps while working with the Power Apps Canvas app.

It will also show you how to patch the Power Apps collection, which includes the following items:

  • How to patch an item to the Power Apps collection?
  • How to patch multiple records to the Power Apps collection?
  • How do patch items to the Power Apps collection dynamically?
  • How to patch the Power Apps collection with lookup()?
  • How to Power Apps refresh the collection after the patch?
  • How to patch a Power Apps collection to excel?
  • How to use a patch to delete collection items within the Power Apps?
  • Power Apps Patch collection yes/no column SharePoint
  • Power Apps Patch collection from the dropdown
  • Power Apps Patch collection to another collection

Recently, we published an article on the Power Apps gallery using the Patch function. Where we discussed what is Power Apps Patch() function is and how to use this function within the gallery in different scenarios. We suggest following the mentioned link to get more ideas about Patch().

Power Apps collection patches a record

In this section, we will see how to patch a record in the Power Apps collection. That means we will patch a new record into an existing collection within the Power Apps.

Assume we have created a collection on a button’s OnSelect property named “colBook” that has some book names, authors, and sales. Display the collected items within a Power Apps data table.

Power Apps patch collection item
Power Apps patch collection item
  • Let’s patch a new item or record within the above collection. For this, add another button control to the screen and set the Text property to the button (Ex-Patch)
  • Insert the below expression on the button’s OnSelect property. As a result, when the user clicks on the patch button, an item will be patched to the Power Apps collection.
OnSelect = Patch(
    colBook,
    Defaults(colBook),
    {
        Name: "Mind F**ked",
        Author: "Anubhav Agrawal",
        Sale: 280
    }
)

where “colBook” is the name of the existing Power Apps collection.

Power Apps patch collection record
Power Apps patch collection record

That’s it! We can see, a new record will be patched within the Power Apps collection, once the button is clicked.

Power Apps patch collection create item
Power Apps patch collection creates item

This is how to use the patch function to add a new item to the Power Apps collection.

Read How to Sort Power Apps Collection

Power Apps Patch collection with lookup

In this section, we will see how to patch a collection with the lookup function. We can use the LookUp function via a patch to modify a specified item within the Power Apps collection.

Let’s take the above example of the Power Apps collection, i.e., colBook. Suppose we want to change the Author from “Rabindra Nath Tagore” to “R.N.Tagore” in the book “Geetanjali.”

Power Apps Patch collection with lookup
Power Apps Patch collection with lookup

For this, insert the below expression in the button’s OnSelect property.

OnSelect = Patch(colBook, LookUp(colBook, Name = "Geetanjali"), {Author: "R.N.Tagore"})
PowerApps Patch collection with lookup
PowerApps Patch collection with lookup

Once the Patch button is clicked, we can see the specified author name will be updated as shown below:

Power Apps Patch update collection
Power Apps Patch update collection

This is how to update an item within the Power Apps collection using Patch.

Power Apps forall patch collection SharePoint list or Power Apps Patch collection multiple records

In this section, we will see how to patch multiple records to the Power Apps collection or how to use the ForAll function within the Power Apps collection to patch multiple records into the SharePoint list.

Suppose, we have a SharePoint list named Vendors List having some columns such as ID, Title, Order Date, and Quantity as shown below:

Power Apps Patch collection multiple records
Power Apps Patch collection multiple records

Now, we will add multiple records to this above Sharepoint data source using Power Apps collection. For this, the required steps are:

  • On the Power Apps screen, add a button control.
  • Set the button’s Text property as Patch Multiple.
  • Insert the below expression on the OnSelect property.
OnSelect = ClearCollect(
    ColVendors,
    Table(
        {
            Title: "Aerotek",
            OrderDate: Date(2022,10,18),
            Quantity: 180
        },
        {
            Title: "PrideStaff",
            OrderDate: Date(2022,11,11),
            Quantity: 135
        },
        {
            Title: "Flexicrew",
            OrderDate: Date(2022,12,14),
            Quantity: 105
        }
    )
);
Patch(
    'Vendors List',
    ForAll(
        Sequence(CountRows(ColVendors)),
        Defaults('Vendors List')
    ),
    ColVendors
);

Where,

  1. ColVendors is the name of a new collection to store multiple records.
  2. Title, OrderDate, and Quantity are the names of the collection header to store the values.
  3. ‘Vendors List’ is the name of the SharePoint data source.
Power Apps forall patch collection SharePoint list
Power Apps forall patch collection SharePoint list

That’s it. We can see that when we click the button, multiple data are patched to the SharePoint list via the Power Apps collection at the same time.

See also  How to Remove line breaks from text in Power Automate?

When you check the specified SharePoint list, you’ll see the newly added records as shown below:

Power Apps collection patch all
Power Apps collection patches all

This is how to patch multiple items to the SharePoint list via the Power Apps collection.

Read How to remove all items from a collection in PowerApps

Power Apps refresh collection after the patch

In this section, we will see how to refresh a Power Apps collection after the patch. Suppose, we are going to use the above-mentioned SharePoint list and build a collection on the Power Apps screen’s OnVisible property named “ColVendorList“. Also, try to display the collected items within a Power Apps gallery control, as shown below:

Power Apps refresh collection after patch
Power Apps refresh collection after the patch

From the above collection, when the user selects any item, it will redirect to the edit page. where the user can edit the properties of the selected item. After that, when the user clicks on a button, the item will patch into the collection and refresh the collection. These modifications only affect the Power Apps collection, not the SharePoint list.

To work around this requirement, the following steps are needed:

  • On the above Power Apps screen, add an edit form control and place it beside the gallery.
  • Connect that edit form with the SharePoint data source i.e., Vendors List.
PowerApps refresh collection after patch
PowerApps Refresh collection after the patch
  • Insert the below expression on the Next icon’s OnSelect property within the vertical gallery. As a result, when the user selects any item it will navigate to the edit form.
OnSelect = Navigate('Vendors Screen', ScreenTransition.Fade, {selectedItem: ColVendorList_Gal.Selected})

Where,

  1. Vendors Screen is the name of the Power Apps screen.
  2. ColVendorList_Gal is the name of the Power Apps vertical gallery.
Refresh PowerApps collection after patch
Refresh the PowerApps collection after the patch
  • Insert the below expression on the edit form’s Item property to let the user edit the selected item’s property.
Item = ColVendorList_Gal.Selected
Power Apps Patch collection from form
Power Apps Patch collection from the form
  • To patch the item within the Power Apps collection, add a button control to that Power Apps screen.
  • Insert the below expression on the button’s OnSelect property.
OnSelect = SubmitForm(VendorsList_Form);
Patch(
    ColVendorList,
    ColVendorList_Gal.Selected,
    {
        'Order Date': VendorsList_Form.LastSubmit.'Order Date',
        Quantity: VendorsList_Form.LastSubmit.Quantity
    }
);
Power Apps collection refresh after patch
Power Apps collection refreshes after the patch

That’s it! Let’s select the second item from the Power Apps gallery (i.e., Gilli). Modify the date to 12/25/2022 and the quantity to 210. When we click on the Patch button, the specified item will be modified only in the Power Apps collection.

PowerApps Patch collection from form
PowerApps Patch collection from the form

This is how to refresh the Power Apps collection after a patch.

Read How to remove items from a collection in Power Apps

Power Apps Patch collection dynamically

In this section, we’ll look at how to use the Power Apps patch collection dynamically. That is, we will dynamically add different types to a Power Apps collection.

Suppose, we have prepared a Power Apps collection on the screen’s OnVisible property named ‘ColEmpoyee‘ having columns such as Name, Join Date and Department. Also, trying to display that collected data within a Power Apps vertical gallery.

Power Apps Patch collection dynamically
Power Apps Patch collection dynamically

To patch the data dynamically into the above collection, the following steps are:

  • Add a text input, date picker control, and dropdown control to the Power Apps screen to insert the employee name, join date, and department respectively.
  • Insert the below expression on the Power Apps dropdown’s Items property to let the user selects their department.
Items = ["IT", "Finance", "Management"]
  • Again, add a button control to the screen and set the Text property as per the need. (Ex: Patch).
  • Insert the below expression on the button’s OnSelect property. As a result, when the user inserts data and clicks on the button, it will patch those items to the Power Apps collection.
OnSelect = Patch(
    ColEmpoyee,
    Defaults(ColEmpoyee),
    {
        Name: Name_Txt.Text,
        JoinDate: JoinDate_DatePicker.SelectedDate,
        Department: Department_DD.Selected.Value
    }
)

Where,

  1. ColEmpoyee is the name of the Power Apps collection that we have created.
  2. Name, JoinDate, and Department are the names of the collection headers.
  3. Name_Txt, JoinDate_DatePicker, and Department_DD are the names of the text input, date picker, and dropdown controls respectively.
PowerApps Patch collection dynamically
PowerApps Patch collection dynamically

That’s all! Let’s preview the app and fill in the controls by inserting desired data. And click on the Patch button. We can see the item got patched in the Power Apps collection and displayed via the above-mentioned gallery.

Patch Power Apps collection dynamically
Patch Power Apps collection dynamically

This is how to patch items dynamically within the Power Apps collection.

See also  PowerApps nested gallery control example

Power Apps collection patch to excel

In this section, we will see how to work with the Power Apps Collection patch in Excel. That means we will see how to use the patch function to update or modify an item within the Excel sheet via the Power Apps collection.

Suppose, we have an Excel sheet named “ProductsTable” that has some random columns with data. Also, we have formatted that sheet into a table as shown below:

Power Apps collection patch to excel
Power Apps collection patch to excel

Using the above Excel table, we have created a collection within the Power Apps named ColProducts and are trying to display that collection via a data table.

PowerApps collection patch to excel
PowerApps collection patch to excel

From the above collection, we will modify the items based on specific criteria within the Excel sheet. That is, if the product availability is “yes” for the entire collection, then it will modify the item whose product name is “Mobile” to “Tablet” and the quantity from 12 to 26.

To meet this requirement, the following steps are:

  • On the above Power Apps screen, add a button control.
  • Insert the below expression on the button’s OnSelect property.
OnSelect = ForAll(
    Filter(ColProducts, Availablity = "Yes"),
    Patch(
        ProductsTable,
        LookUp(ProductsTable, Product= "Mobile"),
        {Product:"Tablet", Quantity: 26}
    )
)
PowerApps collection patch modify item to excel
PowerApps collection patch modify item to excel

Once we click on the button, the item will be modified in the Excel table as shown below:

Power Apps collection patch to excel sheet
Power Apps collection patch to excel sheet

This is how to patch an excel from the Power Apps collection.

Read Power Apps Filter With Date Picker

Power Apps Patch delete collection

In this section, we will see how to remove or delete an item from the Power Apps collection using the Patch function. Suppose, we have a SharePoint list named “Product Model” that has a Yes/No field that specifies whether the item is delivered or not.

Power Apps Patch delete collection
Power Apps Patch delete collection

We have created a collection using the above SharePoint list and trying to display that data via a Power Apps gallery control as shown below:

PowerApps Patch delete collection
PowerApps Patch delete collection

In the above gallery, we have used the Power Apps checkbox control to display the Yes/No column data.

When the user unchecks any item from the gallery, the item will delete from the Power Apps collection and notify the user about the deleted item. For this the following steps are:

  • On the above Power Apps gallery, select the check box control and insert the below expression on the control’s OnUncheck property.
OnUncheck = Patch(ColProductModel, ThisItem, {Delivered: false}); Notify("Item deleted", NotificationType.Success)

Where ColProductModel is the name of the collection that we have created from the SharePoint list.

Power Apps Patch remove collection
Power Apps Patch removes the collection
  • Next, add the below expression on the gallery’s Items property.
Items = Filter(ColProductModel, Delivered = true)
Power Apps Patch delete collection item
Power Apps Patch deletes collection item

That’s it! Let’s preview the app and uncheck an item within the gallery (for example iPhone 12 Pro Max). Once we uncheck the item’s check box, it will remove that item from the gallery and notify the user as shown below:

PowerApps Patch delete collection item
PowerApps Patch deletes collection item

This is how to delete a Power Apps collection item using the patch function.

Power Apps Patch collection yes/no column SharePoint

In this section, we will see how to patch a SharePoint yes/no column to the Power Apps collection. We have created another collection i.e., CollProducts using the above SharePoint list i.e., Product Model where Delivered is the Yes/No type column.

Also, try to display the collected data via a gallery within the Power Apps. For the Yes/No column, we have used a Power Apps radio control.

PowerApps Patch collection yes no column SharePoint
PowerApps Patch collection yes no column SharePoint

As per the requirement, when the user changes the option on the radio control, the respective result will update in the Power Apps collection. For this, insert the below expression on the radio control’s OnChange property.

OnChange = Patch(
    collProduct,
    LookUp(collProduct, ID= Gallery1.Selected.ID ),
    {
        Delivered: If(
 Radio2.Selected.Value="No", false,
             Radio2.Selected.Value="Yes", true
        )
    }
)

Where,

  1. collProduct is the name of the Power Apps collection.
  2. Delivered is the name of the column.
  3. Gallery1 is the name of Power Apps vertical gallery.
  4. Radio2 is the name of the Power Apps radio control.
Patch Power Apps collection yes no column SharePoint
Patch Power Apps collection yes no column SharePoint

That’s all. Let’s change the options for a few items (Ex- 5 items from the first).

How to patch Power Apps collection yes no column SharePoint
How to patch Power Apps collection yes no column SharePoint

We can see those that will be changed in the Power Apps collection below:

How to patch Power Apps collection using SharePoint yes no column
How to patch Power Apps collection using SharePoint yes no column

This is how to patch Power Apps collection using the SharePoint yes no column.

See also  How to delete all items in SharePoint list using Power Automate?

Read How to disable Power Apps date picker

Power Apps Patch collection from the dropdown or SharePoint choice column

In this section, we will see how to patch a Power Apps collection from the dropdown control. There is a choice column (i.e., Manufacturer) inside the above-mentioned Power Apps collection i.e., collProduct.

Below, we are trying to display the collected data within a Power Apps vertical gallery. Also, we have built a dropdown control using the SharePoint choice column.

Power Apps Patch collection from dropdown
Power Apps Patch collection from the dropdown

When a user selects an item from the Power Apps gallery, the default selected choice value is displayed on the dropdown control, where the user can modify the choice as needed. This modification will reflect on the Power Apps collection. For this, the following steps are:

  • Select the dropdown control, on the above Power Apps screen.
  • Insert the below expression on the Default property to display the default selected choice in the control.
Default = 'Product Gallery'.Selected.Manufacturer.Value

where,

  1. Product Gallery is the name of the Power Apps vertical gallery.
  2. Manufacturer is the name of the choice column.
PowerApps Patch collection from dropdown
PowerApps Patch collection from the dropdown
  • To patch the modified choice from the dropdown control to the Power Apps collection, insert the below expression on the OnChange property of the dropdown control.
OnChange = Patch(
    collProduct,
    LookUp(
        collProduct,
        Title = 'Product Gallery'.Selected.Title
    ),
    {Manufacturer: Manufacturer_DD.Selected}
)

Where Manufacturer_DD is the name of the Power Apps dropdown control.

Power Apps Patch collection SharePoint choice field
Power Apps Patch collection SharePoint choice field

That’s all! Let’s select an item from the Power Apps gallery and modify the manufacturer from the dropdown control. (Ex: Apple magic Mouse, from Apple to Other)

PowerApps Patch collection SharePoint choice field
PowerApps Patch collection SharePoint choice field

We can see that once we selected the choice from the manufacturer dropdown control, it was modified that item within the Power Apps collection.

This is how to patch the Power Apps collection with the SharePoint choice field.

Power Apps Patch collection to another collection

In this section, we will see how to patch a Power Collection to another collection. Suppose, we have created 2 Power Apps collections i.e., Collection1 and Collection2 on the buttons’ OnSelect property having some different types of columns.

Whereas Collection1 includes columns like ProductNo and ProductGUID, Similarly, Collection2 also contains some different columns, such as ProductSeq, Vendor, and ProductID, as shown below.

Power Apps Patch collection to another collection
Power Apps Patch collection to another collection

As per the scenario, on the above collections, if the ProductNo (in Collection1) equals the ProductSeq (in Collection2), the ProductID (in Collection2) values will be updated to the ProductGUID (in Collection1). To achieve this requirement, the following steps must be taken:

  • Add a button control to the above Power Apps screen.
  • Set the Text property (Ex-Patch).
  • Insert the below expression on the button’s OnSelect property.
OnSelect = ForAll(Collection1, UpdateIf(Collection2, ProductSeq = ProductNo, {ProductID: ProductGUID}))
PowerApps Patch collection to another collection
PowerApps Patch collection to another collection

Once the patch button is clicked, we can see the ProductGUID will be updated in the ProdductID.

Patch collection to another collection in Power Apps
Patch collection to another collection in Power Apps

This is how to patch a collection to another collection in the Power Apps.

Conclusion

From the Power Apps Tutorial, we learned how to use the patch function within the Power Apps collection based on different scenarios such as:

  • Power Apps patch collection item
  • Power Apps Patch collection with multiple records
  • Power Apps Patch collection dynamically
  • Power Apps Patch collection with lookup
  • Power Apps refresh collection after the patch
  • Power Apps collection patch to excel
  • Power Apps Patch delete collection
  • Power Apps Patch collection to another collection
  • Power Apps Patch collection yes/no column SharePoint
  • Power Apps Patch collection from the dropdown or the SharePoint choice field

You may like the following Power Apps tutorials:

>