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
Solved! Go to Solution.
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” } ))
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.
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” } ))
Stay up tp date on the latest blogs and activities in the community News & Announcements.
Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.
User | Count |
---|---|
208 | |
207 | |
85 | |
58 | |
35 |
User | Count |
---|---|
334 | |
259 | |
132 | |
87 | |
60 |