Duplicate a Record with Lightning Flow in Spring ‘20


#AwesomeAdmin Melissa Hill Dees wondered aloud to the Trailblazer community on Twitter how she could duplicate a Salesforce record while updating some of the fields using only declarative tools. Melissa says, when a record meets a specific criteria, “I want to duplicate the record, adjust some values, and save the new record.” This made me think: While Salesforce doesn’t offer a duplicate record action, there are a couple of different ways to accomplish this. Could I build this? With the new Spring ’20 Flow option to automatically store all record fields, is it possible to accomplish this with three easy steps. I’ve laid out the steps and explained everything below, so let’s take a look at how to get this done with the incredible tools now available to us in Spring ’20.

Here’s the scenario: We run a subscription business, so once an Opportunity closes, we want to create a duplicate renewal Opportunity set with a future close date. Here’s what we want to do at a high level:

When an Opportunity is set to Closed Won:

  • Copy current Opportunity record values
  • Prepend [Renewal] to the Opportunity name
  • Change Close date to 1 year from today
  • Create the new Opportunity record

You could use Process Builder with a Create Record action, but this requires you to define every field, one-by-one. Besides the tedious definition of each field, it also means that any field changes to the object will require updating the process. I prefer solutions that don’t require future work, and you should, too, as an Admin best practice.

Enter Spring ‘20 Flow. To accomplish this task, we need a process, a flow, and a subflow. It sounds like a lot, but each has just a couple of steps. Based on the criteria, the process will launch the flow. Because the new automatic field detection feature is smart enough to only store the actual fields used in the flow, we need to trick it by further passing data into a subflow. This is an important nuance regarding this new feature: Despite saying “store all fields,” the feature only stores what the flow actually needs. Therefore, by passing the data into the subflow, the parent doesn’t know which fields are used, so it pulls in all the fields. Then, the subflow does all the hard work.

Step 1: Create the subflow

Because each piece builds on the other, we need to start at the end, creating the subflow first. This flow modifies the desired field values and creates a new record.

1. In the Toolbox, switch to the Manager and create a new variable resource. Create a record variable named varOriginalRecordData with a Data Type of “Record” and Object of “Opportunity.” Make sure it’s available for input. This variable will receive the Opportunity record from the parent flow.

2. Now that the record variable exists, we can add a formula to modify the Opportunity Name. Click New Resource and Formula. Give the formula a name (fxModifyOpptyName) and data type of Text. The formula I’m using is: "[Renewal] " + {!varOriginalRecordData.Name} .

3. We’ll create another formula to modify the Close Date called fxOneYearFromToday. The data type is Date and formula is: today() + 364 . I realize this formula doesn’t account for leap years; we’re keeping it simple for this post.

4. Next, add an Assignment Element to the canvas. Give it a name (Assign Values). Now, we need to set these field values. What we’re doing is changing just these four fields and leaving all the other Opportunity fields alone:

{!varOriginalRecordData.Name} equals {!fxModifyOpptyName}
{!varOriginalRecordData.CloseDate} equals {!fxOneYearFromToday}
{!varOriginalRecordData.StageName} equals Qualification
{!varOriginalRecordData.Id} equals {!$GlobalConstant.EmptyString}

5. Finally, add a Create Records element to the canvas, giving it a name (Create Records). You’ll create just one record, and set the record field values to use all values from a record. Select the variable {!varOriginalRecordData}.

6. Link all elements together, along with the Start. Save this flow, naming it Duplicate Opportunity Subflow, and don’t forget to Activate it.

Step 2: Create the parent flow

We’ve created the subflow, so now let’s create the flow that calls it. This parent flow will then be launched by Process Builder when the Opportunity record meets the specified criteria.

1. Create a variable to store the incoming record’s ID from Process Builder. Go to Manager and click New Resource. In this case, the variable must be named recordId (and case is important). The Data Type is Text, and make it available for input.

2. Add a Get Records element with API name Get Opportunity. We’ll configure the Get Element to find the record using the variable we created. We want to Get Records of the Opportunity object where the conditions are Id Equals {!recordId}. Select “only store the first record” so that we’re only getting a single Opportunity. Now for the magic! Choose Automatically store all fields — this is the new functionality just released in Spring ‘20. Automatically storing these fields creates a record variable. Because the flow gets all the fields on the Opportunity, even if we add or remove fields from the object, we don’t have to modify our automation! Pretty neat, right?

3. Now let’s add the Subflow element we created earlier to the canvas. Select the subflow we created in Step 1 — “Duplicate Opportunity Subflow.” We want to pass our record variable from this flow into our subflow, so be sure to check the Include box and choose the {!Get_Opportunity} resource.

4. Link all elements together, along with the Start. Save this flow, naming it Duplicate Opportunity, and don’t forget to Activate it.

Step 3: Create a process

Process Builder will launch the flow we just created based on the criteria we specify. Go to Process Builder in Setup. Note: For the purpose of this post, we’re creating a new process. As an admin best practice, you should only have one process per object.

1. Create a New Process that starts when a record changes.

2. Set the Object to Opportunity, and start the process when a record is created or edited.

3. Add Criteria as defined in the requirements. Name the Criteria (Stage = Closed Won). Set the criteria for when conditions are met, which is the field “Stage” equals “Closed Won.”

4. Now add an Immediate Action. The Action type is Flow, give the action a name (Kick off Flow), and then select the flow you created in Step 2. Now, set the Flow Variable for recordId to the field reference of [Opportunity].Id.

5. Save your “Create Renewal Opportunity” process and Activate it.

Wrapping it up

If you’ve done everything correctly, when an Opportunity reaches a Closed Won stage, Process Builder will send the record ID to the Flow Duplicate Opportunity. Duplicate Opportunity will get all of the record’s fields and pass them to the subflow Duplicate Opportunity Subflow. Duplicate Opportunity Subflow will then modify the Name, Close Date, and Stage, and create a new duplicate record. The new Opportunity’s name includes the text “[Renewal]” and a close date one year from now.

Even more Spring ‘20!

You may be wondering how I got these cool, dark Salesforce screenshots. Spring ‘20 also saw the release of the new Lightning Extension for Chrome. One of the great features of the extension is the ability to enable Dark Mode for Salesforce. Check it out!

Learn MOAR with the Salesforce Optimizer App in Summer ‘20

We know that you, as Admins, are always improving productivity for your users by securing data, ensuring your Salesforce instance is clean and free of technical debt, and staying up to date on best practices. However, it can be challenging to find the time for org maintenance. With the new Salesforce Optimizer Lightning app, you […]


Learn MOAR with Summer ’20 Release Updates Setup Page

Discover Summer ’20 Release features! We are sharing five release highlights for admins and developers, curated and published by our evangelists as part of Learn MOAR. Complete the trailmix by July 31, 2020, to get a special community badge and unlock a $10 contribution to Bibliothèques Sans Frontières (Libraries Without Borders). Every Salesforce Release holds […]


Have an Idea for a Story?

We are all about the community and sharing ideas.
Do you have an interesting idea or useful tip that you want to share?