Blog
Forms - How to copy/save an existing record on a form as a new record
May 17. 2021
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
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:
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})
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.- Categories:
- forms
Related posts
- Forms - How to set the value of a field to todays date for new records only
- Apps - How to create an app from a hand drawn image
- Forms - How to highlight user modified field values on a form
- Forms - How to append text to field in a data source
- Forms - How to calculate values (eg sums and products) and store the results in SharePoint or other datasource
- SharePoint - How to programmatically set and clear single select choice items in a combo box on a form
- SharePoint - How to clear datetime fields/set an empty datetime value on a form
- Forms - How to convert a display form to an edit form
- Forms - How to show Office 365 user profile details on a form
- Forms - How to hide fields that are blank, or have not been completed
- Forms - How to select-all / unselect-all checkbox or toggle controls on a form
- Forms - How to set the data source of a form to a collection
- Forms - The best practice for setting the data item on a form
- Controls - How to set default control and form values
- Forms - How to conditionally make form fields mandatory