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

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
Anonymous
Not applicable

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

Hi @Anonymous

 

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
Anonymous
Not applicable

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
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,354)