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

When Using Patch - is there an edit mode?

I know when using forms there is a property which can be set to edit or new form.

Is there a way when not using forms and using the Patch command, of testing for a new or an edit record?

I am trying to use the same screen for both adding records or editing existing ones.

At the moment I am setting a variable to either 'New' or 'Edit' values and checking that when I enter the screen.

Wondered If there is a better way.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Super User II
Super User II

Hi @sienna28 ,

The patch function is different if one is editing a record rather than adding a new one with respect to the second argument of the function.  In the case of adding a new record it is

Patch( Customers, Defaults( Customer ), { Name: “Contoso” } ).

  This syntax creates a new record.  In the case of patching an existing record, instead of Defaults(), you specify the record being patched ie. 

Patch( Customers, Lookup( Customers, Name = "Contoso" ) , { Phone: “1-212-555-1234” } ).

  To determine which one to use, test if the record exists and then which syntax to employ, you can use an If() function combined with an IsBlank().  Something like:

If(IsBlank(Lookup(Customers,Name="Contoso"),Patch( Customers, Defaults( Customer ), { Name: “Contoso” } ),Patch( Customers, Lookup( Customers, Name = "Contoso" ) , { Phone: “1-212-555-1234” } ))

  

View solution in original post

2 REPLIES 2
Dual Super User III
Dual Super User III

There isn't a way to have Patch test to see if a record exists and create a new one if it doesn't.  But there are two different syntax forms for using patch for a new record versus an existing record.  The second parameter of the patch statement to create a new record is always defaults(datasourcename) and the second parameter of the patch statement to edit an existing record is some formula that will select a specific record to be updated.  This is usually a Lookup().  What you can do is embed both forms of the Patch in an IF() statement that checks your New/Edit variable to see which statement to execute.  Normally, even if you want to use the same screen and form you pre-populate the form with either default values (new) or existing values (edit).  So you still need to know whether its a new or exisiting record long before you get to patch.



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

Hi @sienna28 ,

The patch function is different if one is editing a record rather than adding a new one with respect to the second argument of the function.  In the case of adding a new record it is

Patch( Customers, Defaults( Customer ), { Name: “Contoso” } ).

  This syntax creates a new record.  In the case of patching an existing record, instead of Defaults(), you specify the record being patched ie. 

Patch( Customers, Lookup( Customers, Name = "Contoso" ) , { Phone: “1-212-555-1234” } ).

  To determine which one to use, test if the record exists and then which syntax to employ, you can use an If() function combined with an IsBlank().  Something like:

If(IsBlank(Lookup(Customers,Name="Contoso"),Patch( Customers, Defaults( Customer ), { Name: “Contoso” } ),Patch( Customers, Lookup( Customers, Name = "Contoso" ) , { Phone: “1-212-555-1234” } ))

  

View solution in original post

Helpful resources

Announcements
News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

Power Apps Community Call

Power Apps Community Call- January

Check out the Power Apps Community Call for January 2021

PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

Top Solution Authors
Top Kudoed Authors
Users online (7,756)