Hello,
I recently added a 'Copy' button to the main grid in my model-driven app, and here’s how I made it work:
To ensure the button only appears when an item is selected, I used this condition:
!IsEmpty(Self.Selected.AllItems)
Here’s the code I used to copy the selected record, including the lookup field LK_Suppliers
:
IfError(
Patch(
tblProducts, // Data source
Defaults(tblProducts), // Creates a new record
{
LK_Suppliers: Self.Selected.Item.LK_Suppliers,
ProductLabel: Self.Selected.Item.ProductLabel,
ProductTitle: "(copy) " & Self.Selected.Item.ProductTitle, // Adds "(copy)" to the title
ProductQuality: Self.Selected.Item.ProductQuality,
ProductGsm: Self.Selected.Item.ProductGsm,
IsRound: Self.Selected.Item.IsRound,
ProductLength: Self.Selected.Item.ProductLength,
ProductWidth: Self.Selected.Item.ProductWidth,
ProductDiameter: Self.Selected.Item.ProductDiameter,
ProductHeight: Self.Selected.Item.ProductHeight,
ProductImage: Self.Selected.Item.ProductImage
// Add other fields if needed
}
),
Notify("Error: Could not copy the product.", NotificationType.Error),
Notify("Product copied successfully!", NotificationType.Success)
)
Now, when I select a record and click the button, it creates a duplicate of the entry with the specified fields.
Note - I encountered the same issue initially, but it unexpectedly resolved on its own.