Duplicate a Record with Lightning Flow in Spring ‘20

By

#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 About Lightning Flow Goodies Coming in Summer ’20

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). The Salesforce Summer ’20 […]

READ MORE
Spring 20 Flow Builder

Learn MOAR in Spring ’20 with Flow Builder

Discover Spring ’20 Release features! We are sharing five release highlights for Salesforce Admins (and Developers), curated and published by our evangelists and PMs as part of Learn MOAR. Complete the trailmix by March 31, 2020, to get a special community badge and unlock a $10 contribution to FIRST®. With the Spring ’20 Release, building […]

READ MORE

Build a Simple Screen Flow 🧐

Did you know that you can build interactive custom components without any code? Flow Builder is a powerful tool for automation and for giving Admins the power to customize the look and feel of their Salesforce experience. In this short video we will create a simple screen flow that you can surface to your users.

READ MORE

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?

SHARE YOUR IDEA