cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Lourenacdm
Helper III
Helper III

Patch doesnt work in edit form

In my app I need to save the various screens, I'm using the PATCH function below and it's working almost perfectly:

 

Lourenacdm_0-1620309631317.png

I have a problem, as this works only for NEW and does not work when I need to EDIT, when I perform the tests the function is creating a new field every time it is submitted even if it is in the form for editing.

Can someone help me, what can I do to be able to save the multiple screens for Edit and New mode?

1 ACCEPTED SOLUTION

Accepted Solutions
RusselThomas
Microsoft
Microsoft

Hi @Lourenacdm ,

The Patch() statement Syntax is Patch(source, baserecord, changerecord), where "baserecord" is the record in the source you want to change and "changerecord" are the fields for the record you want to update.

If you specify

Defaults(sourceName)

as your baserecord, this means that Patch will always create a new record.  If you want to edit a record, baserecord needs to be the record in the source that you want to change.  This can be obtained by doing a lookup to the source, or referencing a gallery or collection selection/record that is identical to the source record.

 

With the Defaults() new record creation, Patch() doesn't need to look at existing records - it just adds your changerecords to the source as new records.

Editing a record requires Patch() to first find the specific record you want to edit - For this reason, your patch() statement will be different for each record you want to update.

 

In theory, considering your data is coming from Forms, then the record to update for each Form would be the same as that particular Form's Item: property if your Form is in Edit mode. 

As the record to update would be different for each form, this will change how your Patch() statement works and initially looks like three statements instead of one;

Patch(source, [same expression as Form1 Item property], Form1.Updates);
Patch(source, [same expression as Form2 Item property], Form2.Updates);
Patch(source, [same expression as Form3 Item property], Form3.Updates);

Note, you can't just reference the Form Item: property in a function (eg: Form1.Item). 

If you want to turn this into a single patch statement, you can use the following syntax;

Patch(source, basetable, changetable)

Where basetable is a table of source records you want to edit, and changetable is a table of those same source records, just with their field updates.  Your statement might then look something like this;

Patch(source, Table(
             [same expression as Form1 Item property], 
             [same expression as Form2 Item property],
             [same expression as Form3 Item property]),
             Table(
             Form1.Updates, Form2.Updates, Form3.Updates)
);

You just have to make sure the source records and change records appear in the same order and with the same columns as the source.

Example:

Let's assume each form is getting it's record from the same source in a different way - Form1 comes from a gallery selection, Form2 comes from a lookup and Form3 comes from another lookup.

Form1 Item: property 

Gallery1.Selected

Form2 Item: property

LookUp(Source, ID=10)

Form3 Item: property

LookUp(Source, name="bob")

 Then your Patch() statement might look like this;

Patch(source, 
       Table( //basetable records
             Gallery1.Selected, 
             LookUp(Source, ID=10),
             LookUp(Source, name="bob")
             ),

       Table( //changetable records
             Form1.Updates, 
             Form2.Updates, 
             Form3.Updates
            )
);

Another consideration, seeing as you're already using a Form, is to just use SubmitForm() three times.  If the form is in Edit mode, then it will update the relevant record - three calls, but probably a lot simpler - but of course, you might be using Patch specifically for some reason, so totally up to you.

SubmitForm(Form1);
SubmitForm(Form2);
SubmitForm(Form3)

 Hope this helps,

RT

View solution in original post

6 REPLIES 6
Pstork1
Dual Super User III
Dual Super User III

The second parameter of the patch is Defaults() if you want to create a new record. if you want to edit an existing record the second parameter needs to be something that identifies the particular record you want to update.  You need a different patch() for edit than for new.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
RandyHayes
Super User
Super User

@Lourenacdm 

You should be using the features of the EditForm to provide all the logic for updating or creating records.  The EditForms should be submitted with the SubmitForm.  Otherwise you are losing all of the functions and features that a form provides.

Since you are trying to split a form into multiple ones, I would suggest reviewing this video on splitting forms the proper way as to not lose features or functions.

 

I hope this is helpful for you.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
RusselThomas
Microsoft
Microsoft

Hi @Lourenacdm ,

The Patch() statement Syntax is Patch(source, baserecord, changerecord), where "baserecord" is the record in the source you want to change and "changerecord" are the fields for the record you want to update.

If you specify

Defaults(sourceName)

as your baserecord, this means that Patch will always create a new record.  If you want to edit a record, baserecord needs to be the record in the source that you want to change.  This can be obtained by doing a lookup to the source, or referencing a gallery or collection selection/record that is identical to the source record.

 

With the Defaults() new record creation, Patch() doesn't need to look at existing records - it just adds your changerecords to the source as new records.

Editing a record requires Patch() to first find the specific record you want to edit - For this reason, your patch() statement will be different for each record you want to update.

 

In theory, considering your data is coming from Forms, then the record to update for each Form would be the same as that particular Form's Item: property if your Form is in Edit mode. 

As the record to update would be different for each form, this will change how your Patch() statement works and initially looks like three statements instead of one;

Patch(source, [same expression as Form1 Item property], Form1.Updates);
Patch(source, [same expression as Form2 Item property], Form2.Updates);
Patch(source, [same expression as Form3 Item property], Form3.Updates);

Note, you can't just reference the Form Item: property in a function (eg: Form1.Item). 

If you want to turn this into a single patch statement, you can use the following syntax;

Patch(source, basetable, changetable)

Where basetable is a table of source records you want to edit, and changetable is a table of those same source records, just with their field updates.  Your statement might then look something like this;

Patch(source, Table(
             [same expression as Form1 Item property], 
             [same expression as Form2 Item property],
             [same expression as Form3 Item property]),
             Table(
             Form1.Updates, Form2.Updates, Form3.Updates)
);

You just have to make sure the source records and change records appear in the same order and with the same columns as the source.

Example:

Let's assume each form is getting it's record from the same source in a different way - Form1 comes from a gallery selection, Form2 comes from a lookup and Form3 comes from another lookup.

Form1 Item: property 

Gallery1.Selected

Form2 Item: property

LookUp(Source, ID=10)

Form3 Item: property

LookUp(Source, name="bob")

 Then your Patch() statement might look like this;

Patch(source, 
       Table( //basetable records
             Gallery1.Selected, 
             LookUp(Source, ID=10),
             LookUp(Source, name="bob")
             ),

       Table( //changetable records
             Form1.Updates, 
             Form2.Updates, 
             Form3.Updates
            )
);

Another consideration, seeing as you're already using a Form, is to just use SubmitForm() three times.  If the form is in Edit mode, then it will update the relevant record - three calls, but probably a lot simpler - but of course, you might be using Patch specifically for some reason, so totally up to you.

SubmitForm(Form1);
SubmitForm(Form2);
SubmitForm(Form3)

 Hope this helps,

RT

View solution in original post

Hi Rt, 

Thanks for your explanation, I was using the Patch because I have multiple screens for same data table and when I use the Submit Form doesn't work.

RandyHayes
Super User
Super User

@Lourenacdm 

If you want to split your forms and retain the functionality of the form properly - please review the video link I sent you - it will explain it in detail.

 

The suggested way to do this is WAY too complex when it can be done very easily.  Please review that video and let me know if you have any questions.

It will make it all so much simpler for you and you will then be able to actually use your forms properly.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks @Lourenacdm ,

You are most welcome.  It's sometimes tricky to find the correct line between answering the question asked, and making assumptions behind the motivation for questions and perhaps providing a completely different way to get what you want. 

Hopefully at least, if others search for posts on "Patch" and come across yours, while they may not have the same problem, the explanation might help them with their problem.  

 

What is great about this community is that there are plenty of perspectives and people willing to share - I would always recommend exploring all avenues to find the most efficient way to achieve the same result, but regardless of how you apply the final solution to your problem, hopefully you would have learnt something along the way that makes you a better innovator, and even more, willing to share your experience and knowledge with others with empathy when the time comes 😊

 

Kind regards,

RT

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

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.

Users online (1,140)