Blog

Forms - How to copy/save an existing record on a form as a new record

A handy feature is the ability to make a copy of an existing record. The typical use case scenario of this is to speed up, or to simplify data entry. This post describes a technique that we can use to carry out this task.

To simplify data entry, we can add a feature that enables a user to save an existing record as a new record.

This post walks through how add a button to an edit screen that copies the existing record as a new record. To help identify the record, the new record will include the suffx " Copy" in a prominent field.

Pre-requiste steps

As a starting point, we'll build an auto generated app that's based on a SharePoint property list. The schema of this list looks like this:


Using pattern that I described in the link beneath, we'll set a variable that stores the current record on the OnSelect property of the gallery control on the browse screen.

http://powerappsguide.com/blog/post/best-practice-for-setting-form-item

Set(varCurrentRecord, ThisItem)

Next, we modify the Item property of the display and edit screens to reference the varCurrentRecord variable.

Adding a 'Save as copy' button to the edit screen

On the edit screen, we now add a button or icon to save the existing record as a new record. We set the formula in the OnSelect property to the following:

UpdateContext({locCopy:true});
SubmitForm(EditForm1)


This sets a variable called locCopy to indicate that we want to make a copy of the record.

We should initialise this variable to false on the OnVisible property of the screen:

UpdateContext({locCopy:false})

Next, we modify the Item property of edit form like so:

If(locCopy=true, 
Patch(varCurrentRecord, {ID:Blank()})
varCurrentRecord
)

This formula tests the value of the locMakeCopy variable. If this is true, the call to patch blanks out the primary key, ID value. This is handy tip that I picked up from Randy Hayes. When the SubmitForm function encounters an empty primary key value, it adds the record as a new record, even if the FormMode of the form is set to Edit, rather than New.

If the locMakeCopy variable is false, we set the Item property to varCurrentRecord to support the ability to update the existing record.

Finally, we can optionally modify the update property of the Address1 card like so:

If(locCopy=true, 
DataCardValue9.Text & " (copy)",
DataCardValue9.Text
)

This configures the card to append the word " (copy)" to the value that it saves to the SharePoint/data source when the value fo the locMakeCopy variable is true. In this example, DataCardValue9 refers to the text input control within the Address1 card.

Conclusion

To simplify data entry, we can add a feature that creates a copy of an existing record. This post described a technique that we can apply to an edit screen with a form. The key step is to blank the primary key value. This configures the SubmitForm function to add the record as a new record, rather than update the existing record.