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

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

 

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

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

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."

 

 

 

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

Neat!

hpkeong
Community Champion
Community Champion

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

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

hpkeong
Community Champion
Community Champion

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

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

yegdeveloper
Advocate II
Advocate II

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
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (25,777)