cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mr-dang
Community Champion
Community Champion

Patch a Collection

The release notes for 2.0.531 indicates an improvement to the Patch function that I'm excited about: 

 

 

7. Update multiple records at once by using the Patch function.

Create or update more than one record at a time by specifying a table of base records and 
a table of change records as arguments for the Patch function.

 

I think this is what I've been waiting for, but I'm not sure how to use it. 

 

Can I save a Collection with identical fields back to the datasource? For instance, can I Patch the rows in Collection1 into DataSource1 if they have identical columns? I've been trying:

 

Patch(DataSource1,Collection1)

 

but that does not result in any new rows written. What am I doing wrong?

 

 

EDIT: could it be that the default fields (Title, CreatedOnDateTime, etc.) are blank in the Collection, so it cannot be Patched with those blank?

Microsoft Employee
@8bitclassroom
39 REPLIES 39
Meneghino
Community Champion
Community Champion

You are welcome.

The way I understand it, it is cleaner to use AddColumns instead of ForAll when you just need to calculate a value.  I reserve ForAll for when there is a real need to use actions.

Meneghino
Community Champion
Community Champion

You are right, @Mike8, only the second part.

Here is the first part, with a bit of a cheat...

ClearCollect(Collection1,{Letter:"A",Number:1},{Letter:"B",Number:2},{Letter:"C",Number:3});
ClearCollect(Collection2,{SecondNumber:4},{SecondNumber:5},{SecondNumber:6});
ForAll(Collection2, Patch(Collection1, Last(FirstN(Collection1, SecondNumber-3)), {Number: SecondNumber}))

If you don't want to cheat, just add an index to both collections.

Mike8
Memorable Member
Memorable Member

Perfect. Thank you again @Meneghino.

Especially your first answer about the second part of my question really helped me.
I was using this:
ClearCollect(Collection3, AddColumns(Collection1, "New_column", Collection2.SecondNumber))
and I had the Collection2.SecondColumn in every record in a nested structure.
Problem solved. 🙂

kullurumanoj
Helper III
Helper III

 

This is the property I have setup to add data to sql server..getting error ; the first argument of patch should be a collection

 

Patch('[dbo].[Sample]',Defaults('[dbo].[Sample]'),
{
Organisation : DataCardValue15.Text,
Line_of_Business :DataCardValue16.Text,
Industry :DataCardValue18.Text ,
Director :DataCardValue19.Text ,
Product :DataCardValue20.Text ,
Engineering_Manager :DataCardValue21.Text,
Metrics_Reporter : DataCardValue22.Text })

Hi @kullurumanoj

I believe the error is related to your table structure.  The SQL table should have a primary key defined.

For example, see here:

https://powerusers.microsoft.com/t5/General-Discussion/Patch-problems-with-on-premises-SQL-gateway/m...

 

Heyy bro.. thanks it did work and the problem is when I try to edit the previous record ,a new record with the edited field is getting created instead of updating old record.

What happens depends on the second parameter of the Patch.

Use defaults to create a new record, or a simple record with primary key values to edit an existing record.

Like this:

Patch(DataSource, {ID: 123}, {TextColumn: "Foo"})

PS You can get better performance if you avoid the use of Defaults():

https://baizini-it.com/blog/index.php/2018/01/11/powerapps-replace-the-defaults-function-to-improve-...

hey my primary key is UID,but whenever I try to edit other fileds it says The item has already been created on the server, but able to edit UID and also it gets created as new record with edited UID,rest with same properties..I am a beginner need some help with this

 

Here is my code ;

 

Patch('[dbo].[Sample]',{UID:Blank()},
{UID : DataCardValue21.Text,
Organisation : DataCardValue22.Text,
Line_of_Business : DataCardValue23.Text,
Industry : DataCardValue24.Text ,
Director : DataCardValue25.Text ,
Product : DataCardValue26.Text ,
Engineering_Manager : DataCardValue27.Text,
Metrics_Reporter : DataCardValue28.Text });
Navigate(BrowseScreen1, ScreenTransition.None);Refresh('[dbo].[Sample]')

Hi @kullurumanoj

If you are still having issues please get in touch via private message and I will try to see what is going on.

Thanks.

Was able to figure that out ..Thanks for the help

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

secondImage

Power Apps Community Call

Please join us on Wednesday, October 20th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Users online (1,301)