cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MRCL
Level 8

How to bulk update with ForAll or Patch

I was excited to see bulk update promoted in the latest version. However there is no single example of the syntax needed in the documentation. I tried some obvious alternatives but could not make it work. Please, if you have taken so much effort to include the feature, make a little more effort to show us how it works.

1 ACCEPTED SOLUTION

Accepted Solutions
Power Apps Staff rgruian
Power Apps Staff

Re: How to bulk update with ForAll or Patch

 

We are working on improving the documentation for all of this. The latest can always be found here:

 

Patch:

https://powerapps.microsoft.com/en-us/tutorials/function-patch/

 

ForAll:

https://powerapps.microsoft.com/en-us/tutorials/function-forall/

 

There are multiple ways to use multi-patch. Here's one way:

 

    Patch(datasource, table)

 

The second parameter specifies a table of records to push into 'datasource'. Each row in 'table' must have at least a value for the primary key of 'datasource' -- that's how Patch will find the records in 'datasource' that need to be patched. The rest of the fields in each row in 'table' specify the values to be pushed into the corresponding row in 'datasource'. Example:

 

    Patch(Cars, Table( { Id: 12, Brand: "Audi", Model: "A7" }, { Id: 24, Brand: "Volvo", Model: "S6" } ))

 

The ForAll function allows for the evaluation of an expression on every row of a data source. The expression can be side-effect free, such as a simple Sum, or it can be a possibly chained expression consisting of imperative actions, such as Collect. The ForAll invocation will assemble a table of results, one per expression at every row. Some examples:

 

    ForAll(Squares, Sqrt(Value))

    ForAll(Squares, Collect(datasource, { Square: Value }))

    ForAll(Cars, If(Brand = "Volvo", Collect(VolvoCars, { Name: Model, Year: Year })))

 

I hope this helps.

 

 

Radu Gruian [MSFT] ** PowerApps Staff

View solution in original post

13 REPLIES 13
mr-dang
Level 10

Re: How to bulk update with ForAll or Patch

I just posed the same question on the forum 🙂 It goes to show you how popular this feature is--it will be a game changer if I can get it to work. 

 

I can publish a Camtasia video for the forum if I can get some examples of the syntax first. @rgruian, can you elaborate on the bulk Patching?

Microsoft Employee
@8bitclassroom
MRCL
Level 8

Re: How to bulk update with ForAll or Patch

Absolutely. It is the last piece of the puzzle to create functional ocasionally connected mobile apps.

The blog was very esplicit about the new capabilities.

 

"Finally, one of the reasons you gave us for needing iteration was the bulk update of data.  And ForAll can be used for that.  But as we think it may be common, we also added the ability to use the Patch function on a set of records instead of just modifying one record at a time."

 

 

Power Apps Staff rgruian
Power Apps Staff

Re: How to bulk update with ForAll or Patch

 

We are working on improving the documentation for all of this. The latest can always be found here:

 

Patch:

https://powerapps.microsoft.com/en-us/tutorials/function-patch/

 

ForAll:

https://powerapps.microsoft.com/en-us/tutorials/function-forall/

 

There are multiple ways to use multi-patch. Here's one way:

 

    Patch(datasource, table)

 

The second parameter specifies a table of records to push into 'datasource'. Each row in 'table' must have at least a value for the primary key of 'datasource' -- that's how Patch will find the records in 'datasource' that need to be patched. The rest of the fields in each row in 'table' specify the values to be pushed into the corresponding row in 'datasource'. Example:

 

    Patch(Cars, Table( { Id: 12, Brand: "Audi", Model: "A7" }, { Id: 24, Brand: "Volvo", Model: "S6" } ))

 

The ForAll function allows for the evaluation of an expression on every row of a data source. The expression can be side-effect free, such as a simple Sum, or it can be a possibly chained expression consisting of imperative actions, such as Collect. The ForAll invocation will assemble a table of results, one per expression at every row. Some examples:

 

    ForAll(Squares, Sqrt(Value))

    ForAll(Squares, Collect(datasource, { Square: Value }))

    ForAll(Cars, If(Brand = "Volvo", Collect(VolvoCars, { Name: Model, Year: Year })))

 

I hope this helps.

 

 

Radu Gruian [MSFT] ** PowerApps Staff

View solution in original post

MRCL
Level 8

Re: How to bulk update with ForAll or Patch

Neat!

hpkeong
Level 10

Re: How to bulk update with ForAll or Patch

Hi MRCL:

 

You may refer to my latest post:

Useful Features of PowerApps (15): Multiple Records ....

for your reference if you wish, for your reference.

 

TQ

hpkeong
ramirezp
Level 8

Re: How to bulk update with ForAll or Patch

Where exactly do we type in the 'Patch' formula in the GUI?  Thanks!

hpkeong
Level 10

Re: How to bulk update with ForAll or Patch

Hi @ramirezp

 

Maybe my Useful Features of PowerApps ; Multi-Patch and ForAll is not clear.

In order to trigger Patch or ForAll, you can always use them with:

- Button. OnSelect

- Timer1.OnTimerEnd

- and any other behavior formula in which you OnSelect, OnChange, OnCheck, OnUnCheck, and even If, UpdateIF.

 

Hoep this helps.

hpkeong
ramirezp
Level 8

Re: How to bulk update with ForAll or Patch

Thanks so much hpkeong! That totally cleared things up for me 🙂

yegdeveloper
Level: Powered On

Re: How to bulk update with ForAll or Patch

RECIPE: ADDING MULTIPLE NEW RECORDS IN A COLLECTION TO A DATASOURCE

 

Use "Collect" instead of "Patch" to append multiple new records to an existing datasource. The collection has to have the same field names as the datasource. 

 

The format is:

 

Collect ( DataSourceName , CollectionName )

 

 

 

EXAMPLE

 

If MyFruitCollection is a collection with the following records:

 

{ FruitID : 7, FruitName: "Apple"},
{ FruitID : 8, FruitName: "Banana"},
{ FruitID : 9, FruitName: "Pear"}

 

And MyFruitDataSource a datasource containing these records:

 

{ FruitID : 1, FruitName: "Grape"},
{ FruitID : 2, FruitName: "Orange"},
{ FruitID : 3, FruitName: "Tomato"}

 

Then the call:

 

Collect ( MyFruitDataSource, MyFruitCollection )

will update MyFruitDataSource to be:

 

{ FruitID : 1, FruitName: "Grape"},
{ FruitID : 2, FruitName: "Orange"},
{ FruitID : 3, FruitName: "Tomato"},
{ FruitID : 7, FruitName: "Apple"},
{ FruitID : 8, FruitName: "Banana"},
{ FruitID : 9, FruitName: "Pear"}

 

 

... You would think that:

 

Patch ( MyDataSource , Defaults ( MyDataSource ) , MyCollection )

 

would work, but it doesn't.

 

A constructor for this Patch signature hasn't been implemented in PowerApps. Rather, you get a type error where Patch is expecting a Record in the third argument but gets a Table (MyCollection) instead.

Helpful resources

Announcements
thirdimage

Coming Soon: T-Shirt Design Contest

Keep your eyes open for our upcoming T-shirt design contest!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

Top Solution Authors
Top Kudoed Authors
Users online (6,566)