cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User
Super User

Re: When Using Patch - is there an edit mode?

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
Highlighted
Dual Super User II
Dual Super User II

Re: When Using Patch - is there an edit mode?

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.
Highlighted
Super User
Super User

Re: When Using Patch - is there an edit mode?

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
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

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