cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MRCL Advocate IV
Advocate IV

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
Power Apps

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 Community Champion
Community Champion

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 Advocate IV
Advocate IV

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
Power Apps

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 Advocate IV
Advocate IV

Re: How to bulk update with ForAll or Patch

Neat!

hpkeong Community Champion
Community Champion

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 Kudo Collector
Kudo Collector

Re: How to bulk update with ForAll or Patch

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

hpkeong Community Champion
Community Champion

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 Kudo Collector
Kudo Collector

Re: How to bulk update with ForAll or Patch

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

yegdeveloper Advocate II
Advocate II

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
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Power Platform 2020 release wave 1 plan

Power Platform 2020 release wave 1 plan

Features releasing from April 2020 through September 2020

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