Add Custom Duplicate Buttons with Power Fx in Model-Driven Apps
Learn how to extend forms and grids with a duplicate command using only Power Fx — no JavaScript needed.
Model-driven apps have always allowed makers to customize the command bar, but that used to require JavaScript and the Ribbon Workbench. The modern command designer now accepts Power Fx — the same language used in canvas apps — so almost anyone can add custom buttons without writing a line of JavaScript. This article walks through a real-world scenario: adding a Duplicate button to both a form and a grid view using only Power Fx.
The IT Asset Scenario
Contoso’s IT team manages thousands of assets. When a new laptop arrives, it’s often identical to an existing one except for the serial number. Instead of filling out a form from scratch, they want a single click that copies all fields from the current record. The same logic applies to the grid view when multiple similar assets need to be duplicated.
The Dataverse table is called Asset and has these columns:
| Column Name | Data Type | Example Values |
|---|---|---|
| AssetName | Text | "Dell Latitude 5520" |
| Category | Choice | Laptop, Monitor, Keyboard, Mouse, Headset |
| SerialNumber | Text | SN-LAT-2024-001 |
| PurchasePrice | Currency | 1200.00 |
After creating the table and adding a few sample rows, you can build a model-driven app that includes the Asset table with its main form and grid view.
Setting Up the Command Designer
- In your model-driven app, navigate to the table Asset.
- Click the … (more commands) next to the table name, then choose Edit command bar.
- You will see two contexts: Main form and Main Grid. We’ll start with the form.
Adding a Duplicate Button to a Form
In the command designer for the Main form:
- Select New command from the left menu.
- Set the Label to “Duplicate”.
- Change the Icon to “Copy” (the standard copy icon).
- Click Open Formula Bar on the right side.
Now paste the following Power Fx formula. It uses Patch with Defaults(Assets) to create a new record and maps each field from the currently displayed record (Self.Selected.Item).
Patch(
Assets,
Defaults(Assets),
{
AssetName: Self.Selected.Item.AssetName,
Category: Self.Selected.Item.Category,
SerialNumber: Self.Selected.Item.SerialNumber,
PurchasePrice: Self.Selected.Item.PurchasePrice
}
)Defaults(Assets) returns an empty record for the Asset table. It ensures Patch creates a new record instead of updating the existing one.
After saving and publishing the command bar, play the app. Open any asset record, click the Duplicate button, and a new record is created with the same values. The app navigates to the new record’s form automatically.
Adding a Duplicate Button to a Grid View
Return to the command designer and select Main Grid for the Asset table. Create a new command with the same label and icon.
In the formula bar, we need to iterate over all selected rows. Self.Selected.AllItems returns a collection of the selected records. The ForAll function loops through them and Patch creates a copy for each.
ForAll(
Self.Selected.AllItems As AssetRecord,
Patch(
Assets,
Defaults(Assets),
{
AssetName: AssetRecord.AssetName,
Category: AssetRecord.Category,
SerialNumber: AssetRecord.SerialNumber,
PurchasePrice: AssetRecord.PurchasePrice
}
)
)Controlling When the Button Appears
A grid command should only be visible when at least one row is selected. Set the Visible property of the command to:
!IsEmpty(Self.Selected.AllItems)
Publish the command bar. In the grid view, the Duplicate button now appears only after you select one or more rows. It duplicates every selected asset.
Security and Permissions
The custom command runs with the permissions of the signed-in user. To use the Duplicate button, the user must have Write privileges on the Asset table. If the environment has multiple security roles, make sure the command is not hidden by a role-based visibility formula (you can add a condition under Show on condition from formula if needed).
Because the command performs a Patch operation on the server, delegation is not a concern — but ForAll over Self.Selected.AllItems processes records in memory. Very large selections (hundreds of rows) may be slow. Keep selections reasonable.
Common Mistakes and Troubleshooting
| Symptom | Likely Cause | Solution |
|---|---|---|
| Button does not appear on the form | Command not published or visibility condition too restrictive | Publish again; check that Visible returns true for the form context |
| Formula returns an error | Misspelled field name or wrong table name in Defaults | Verify column logical names in the formula match those in Dataverse |
| Only one record is duplicated in grid | Using Self.Selected.Item instead of ForAll | Replace with the ForAll pattern shown above |
| New record is created but data is blank | Defaults(Assets) was omitted | Add Defaults(Assets) as the second argument of Patch |
Final Recommendation
The modern command bar designer with Power Fx is a major step forward for low‑code customization. For simple operations like record duplication, it replaces the complexity of the Ribbon Workbench entirely. If your logic grows beyond a few Patch calls, consider moving it to a Power Automate flow or a custom page, but for most command bar scenarios Power Fx is more than sufficient.
References
- Original source article: Customize The Command Bar In A Power Apps Model-Driven App by Matthew Devaney
- Microsoft Learn: Customize the command bar in model-driven apps (placeholder for exact URL)
- Microsoft Learn: Power Fx overview
Learn how small coding practices—like using descriptive names, flattening conditions, and simplifying logic—can make your apps easier to update and less error-prone.
Move past the gallery and discover the hidden patterns for validation, navigation, and smart submission handling in your data entry forms.
PowerShell unlocks admin capabilities that the Power Platform admin center simply doesn’t offer—from recovering deleted apps to blocking trial licenses. Here’s how to wield them safely.