How to Clone Dataverse Records with Power Fx in Model-Driven Apps

If you or someone in your role has to create the same kind of records manually multiple times, you may have already hated doing it. So, I came up with something to solve this problem.

For example, if you are a salesperson who has to create orders with almost the same data every time, with only minimal changes, you still need to create a new record from scratch. You may have wished there was a Clone option available. So, I developed a cloning functionality using a command button and Power Fx.

I have already posted a blog on cloning a record in a Model-Driven App using a command button and JavaScript. If you want to clone a record using JavaScript, you can refer to my blog on Clone a record in Dynamics 365 CE using JavaScript.

However, there is another way to achieve this. Yes, we can use Power Fx instead of JavaScript to clone a record. Without much complexity, this can be done easily using Power Fx.

As a demonstration, I created a command button named Clone Account on the Account form. In the action, select Run Formula and click Open Formula Bar. Once the formula bar opens, use the Power Fx code below, then save and publish it.

C#
// Clone Account record without related records
If(
    // show confirm dialog to clone the cloned record. 
    Confirm(
        "Are you sure you want to Clone this record?",
        {
            Title: "Clone Confirmation",
            Subtitle: "This action can't be undone.",
            ConfirmButton: "Yes",
            CancelButton: "No"
        }
    ),
    // show progress notification
    Notify(
        "Cloning record...",
        NotificationType.Information,
        3000
    );
    With(
    {
        ClonedAccount:Patch(
        Accounts,
        Defaults(Accounts),
        {
            'Account Name': "Copy - " & Self.Selected.Item.'Account Name',
            'Main Phone': Self.Selected.Item.'Main Phone',
             Website:Self.Selected.Item.Website,
            'Address 1':Self.Selected.Item.'Address 1',
            Email:Self.Selected.Item.Email,
            Fax:Self.Selected.Item.Fax
        }
        )
    },
    // success message after cloning
    Notify(
        "Record cloned successfully.",
        NotificationType.Success,
        5000
    );
    //show confirm dialog to open the cloned record.
   If(
    Confirm(
        "Are you sure you want to open Cloned record?",
        {
            Title: "Open confirmation",
            ConfirmButton: "Yes",
            CancelButton: "No"
        }
    ),
    // navigate to cloned account
    Navigate(ClonedAccount);
);   
),
    // if cloning failed, show error
    Notify(
        "Clone operation cancelled.",
        NotificationType.Error,
        2000
    )
)

If you want to clone a record along with its child records, you can do that as well. For example, if you want to clone an order along with the same set of products, you can use the Power Fx code below to achieve that.

C#
// Clone account with related contacts 
If(
    Confirm(
        "Are you sure you want to Clone this record?",
        {
            Title: "Clone Confirmation",
            Subtitle: "This action can't be undone.",
            ConfirmButton: "Yes",
            CancelButton: "No"
        }
    ),
    //show progress notification
    Notify(
        "Cloning record...",
        NotificationType.Information,
        3000
    );
    With(
    {
        ClonedAccount: Patch(
            Accounts,
            Defaults(Accounts),
            {
                'Account Name':  "Copy - "&Self.Selected.Item.'Account Name',
                'Main Phone': Self.Selected.Item.'Main Phone',
                'Address 1: City': Self.Selected.Item.'Address 1: City',
                'Address 1: Street 1': Self.Selected.Item.'Address 1: Street 1',
                Website: Self.Selected.Item.Website
            }
        )
    }, Patch(
        Contacts,      
        ForAll(
            Filter(Contacts, 'Company Name' = Self.Selected.Item),
            {
                'First Name': ThisRecord.'First Name',
                'Last Name': ThisRecord.'Last Name',
                'Business Phone': ThisRecord.'Business Phone',
                'Email': ThisRecord.'Email',
                'Company Name': ClonedAccount
            }
        )
    );
    Notify("Record cloned successfully.", NotificationType.Success, 5000);
     If(
        Confirm(
        "Are you sure you want to open Cloned record?",
        {
            Title: "Open confirmation",
            ConfirmButton: "Yes",
            CancelButton: "No"
        }
    ),
    Navigate(ClonedAccount);
    ); 
    ),
    // if cloning failed, show error
    Notify("Clone operation cancelled.", NotificationType.Error, 2000)
)

Refer to the GIF below for a demonstration.

Clone record using Powerfx model driven apps

References: