Blog
Data - How to make a copy of a record
A common requirement is to make a copy of a record, based on an existing record. This post describes some examples of how to carry out this task, including how to copy an existing record from a form, a gallery control, and how to write formula that copies the last created record.
1 - Making a copy of record from a form
Let's start by modifying an existing edit screen with an edit form. We'll add button that copies the contents of the form into a new record.
To carry out this task, we add a button and add the following formula to the OnSelect property.
Patch(Client, Defaults(Client), EditForm1.Updates)
- The data source (Client in this example).
- A record to add or update. Here, we specify a new record by calling the Defaults function.
- The fields to add or update. Here, we specify the
Updates property of the edit form. This property returns the contents of all items on the form.
2 - Copying a record from a gallery control
Another place where we can add a 'copy record' feature is on a gallery screen.To build this feature, we can add a button to the item template of a gallery, and add the following formula to the OnSelect property.
Patch(Client,
Defaults(Client),
{
Firstname:ThisItem.Firstname & "(Copy)",
Surname:ThisItem.Surname,
Address1:ThisItem.Address1
}
)
To extend this technique, we can improve this formula by adding some basic error checking, and success/error notifications for the user.
If(IsBlank(Patch(Client,
Defaults(Client),
{
Firstname:ThisItem.Firstname & "(Copy)",
Surname:ThisItem.Surname,
Address1:ThisItem.Address1
}
).ID),
Notify("Error",NotificationType.Error),
Notify("Record copied successfully", NotificationType.Success)
)
3 - Copying the last created record
To add a feature that enables a user to copy the last created record, we can add a button to a screen and add the following formula to the OnSelect property.
With(
{
lastRec: First(
FirstN(
SortByColumns(Client, "ID", Descending),
1
)
)
},
Patch(Client,
Defaults(Client),
{
Firstname:lastRec.Firstname & "(Copy)",
Surname:lastRec.Surname,
Address1:lastRec.Address1
}
)
)
As with our previous examples, this method relies on the patch function. From inside the 'with' block, we look up the last record and expose it through the lastRec identifier. We build a record that we pass as the third argument to the patch function, and we use lastRec to reference the last created record.
4 - Copying the last created record by user, or some other criteria
With(
{
lastRec: First(
FirstN(
SortByColumns(
Filter(
Client,
'Created By'.Email = "timl@powerappsguide.com"
),
"ID",
Descending
),
1
)
)
},
Patch(Client,
Defaults(Client),
{
Firstname:lastRec.Firstname & "(Copy)",
Surname:lastRec.Surname,
Address1:lastRec.Address1
}
)
)
http://powerappsguide.com/blog/post/sharepoint-how-to-filter-records-by-the-current-user
Conclusion
- Categories:
- sharepoint
- patch
- SharePoint - Deleting the last record from a SharePoint list - how NOT to do this!
- Walkthrough - An beginners guide to building an app that stores images in SharePoint
- SharePoint - What to do when there's a mismatch between times in Power Apps and SharePoint
- SharePoint - Filtering lists by User() is now delegable
- SharePoint - How to fix the "skip to main content" error message on integrated forms, or to fix forms where records don't save
- SharePoint - Use this trick to perform a 'contains' type search in a more delegable way
- Sharepoint - Filtering records by yes/no columns bug - now fixed!
- Configuration - How to set the SharePoint address of a data source with a variable
- SharePoint - How to export and import lists and maintain lookup relationships
- SharePoint - how to fix list threshold errors when working with very large lists
- Data - How to move SharePoint sites, lists, and data
- Code - Can we hack SharePoint ID columns to be delegable using the greater than/less than operators?
- SharePoint - How to filter records by the current user
- SharePoint - Beware of numeric calculated columns!