Automatically Clone a Record if it Meets Certain Criteria

Automatically Clone a Record if it Meets Certain Criteria

Last Updated on April 3, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

How to use Salesforce Flow to automate business scenarios that involve cloning an existing record. 

Objectives:

After reading this blog, you’ll be able to: 

  • Understand how to clone a record by using the create record element
  • Use decision element to validate the data
  • Use formulas in the flow to dynamically generate name and date
  • Concatenate 2 fields in the flow formula
  • and much more

Business Use case

Steve Cartwright is working as a System Administrator at Universal Containers (UC). He received a requirement to auto-create a new Opportunity (Similar to an old record) whenever an Opportunity is successfully closed with the following details: 

  • Set the Close date for the new Opportunity to 365 days from today
  • Append + sign to Opportunity Name
  • Stage = Prospecting

Automation Champion Approach (I-do):

Usually, with Salesforce multiple approaches are available to solve a business requirement. Choose the ones that are simple, straightforward, and consume fewer resources. 

Let’s solve this scenario using Salesforce Flow, as this business case involves only one level of cloning. To implement a deep clone – Renewal Opportunity with Line Items (Multilevel cloning), either Salesforce Flow or Apex Programming can be used. 

To solve this requirement, we will use the After-save Record-Triggered Flow. Check out this article to understand why we are using after-save record-triggered flow for this scenario.

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 5 steps to solve Steve’s business requirement using Record-Triggered Flow. We must:

  1. Define flow properties for record-triggered flow
  2. Create a Formula field to generate a close date for the renewal opportunity 
  3. Create a Formula field to generate a name for the renewal opportunity 
  4. Add a Decision element to validate opportunity Stage equals Closed Won
  5. Add a Create Records element to generate a renewal opportunity

Step 1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create and configure the flow as follows:
    1. Object: Opportunity
    2. Trigger the Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Action and Related Records
  5. Click Done.

Step 2: Create a Formula to Generate Close Date For the Renewal Opportunity 

  1. Under Manager click New Resource to create a formula for renewal opportunity close date.
  2. Input the following information: 
    1. Resource Type: Formula
    2. API Name: forD_RenewalOpportunityCloseDate
    3. Data Type: Date
    4. Formula: {!$Record.CloseDate} + 365
  3. Click Done.

Step 3: Create a Formula to Generate Name for the Renewal Opportunity 

  1. Under Manager click New Resource to create a formula for renewal opportunity name.
  2. Input the following information: 
    1. Resource Type: Formula
    2. API Name: forT_RenewalOpportunityName
    3. Data Type: Text
    4. Formula: {!$Record.Name}+’ +’
  3. Click Done.

Step 4: Using Decision Element to Check if Opportunity is Closed Won or Not

Now we will use the Decision element to check the StageName to ensure that it is equal to Closed. 

  1. On Flow Designer, click on the + icon and select the Decision element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$Record.StageName}
      2. Operator: Equals 
      3. Value: Closed Won

Step 5: Add a Create Records Element to Generate Renewal Opportunity 

 The next step is to create a new Opportunity, for this we will use the Create Records element.

  1. On Flow Designer, below the Closed Won node, click on the +icon and select the Create Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: One
    3. How to Set the Record Fields: Use separate resources, and literal values
    4. Object: Opportunity
    5. Set Field Values for the Opportunity
    6. Row 1:
      1. Field: CloseDate
      2. Value: {!forD_RenewalOpportunityCloseDate}
    7. Click Add Field
    8. Row 2:
      1. Field: Name
      2. Value: {!$forT_RenewalOpportunityName}
    9. Click Add Field
    10. Row 3:
      1. Field: Pricebook2Id
      2. Value: {!$Record.Pricebook2Id}
    11. Click Add Field
    12. Row 4:
      1. Field: RecordTypeId
      2. Value: {!$Record.RecordTypeId}
    13. Click Add Field
    14. Row 5:
      1. Field: StageName
      2. Value: Prospecting
    15. Click Add Field
    16. Row 6:
      1. Field: AccountId
      2. Value: {!$Record.AccountId}
  4. Click Done.

In the end, Steve’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 54
  5. Interview Label: Generate Renewal Opportunity {!$Flow.CurrentDateTime}
  6. Click Save.

Almost there! Once everything looks good, click the Activate button.

Proof of Concept

From now onwards, a new opportunity will be automatically created whenever an opportunity is updated as Closed Won. 

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

 

19 thoughts on “Automatically Clone a Record if it Meets Certain Criteria

  1. Hi Rakesh, love the blog.

    Would you mind please explaining the logic behind the
    Value: [Opportunity].Name +” + “ formula for the New Opportunity Name?

    Could it also make sense to use Value: [Opportunity].Name + Renewal?

    I do not understand the use of + and “.

    Thanks,
    Eric

  2. What if we keep closedate as “Closed-Won” other than “Prospecting” on our clone opportunity in process builder. What happens then ?
    Salesforce is going to create more and more opportunities. If happens how can we stop it ? What are the limitations ?

    Thanks for the amazing blog…..Cheers

  3. Hi Rakesh,

    I didn’t create formula field on Opportunity object, Instead I have done this process following way and it’s working fine.
    Under Immediate Action
    Field = Close Date
    Type = Formula
    Value = : [Opportunity].CloseDate + 120.

  4. So it is not truly a clone, but just a creation of a new Opportunity. Same with a Flow, if you want a true clone you’ll have to list out all the fields you want included in Step 7?

  5. Thank you so much for this post. I’ve used it to change the record type on a cloned custom object. I was able to change the Record Type field, but how can I make other field “re-set” to blank? I thought it would be $GlobalConstant.Null, but that is incorrect. Thank you for you help!

    1. You have few options

      1. Use $GlobalConstant.EmptyString
      2. Use a Variable with empty string and refer it
      3. Create a Formula field with empty string then use it
      4. Refer the field in Record Create element and don’t enter anything against it
  6. This is great!! Only questions – when I try this, the cloned opp does not take the Account name, is there anything I have to do differently to accomplish that?

  7. Rakesh –

    I think now we can clone the Opportunity and set the closed date on the cloned opportunity by using formula : [Opportunity].CloseDate + 120. No need to create any additional field in Opportunity object. The above use case I have tried on my developer org without using any additional field and it is working fine now.

    Regards,
    Sudipta Deb

  8. Rakesh,

    Thank, great articles. Regarding multilevel cloning in process builder, could I use multiple sequential time initiated clones, with a counter field, to stop the sequencing when needed, to effect the result of multilevel cloning in apex?

    Thanks
    Pete

  9. Rakesh, when creating a record can you merge text+field data?

    For example in your step 7. is it possible to push a value into a field such as:
    ” The close date is: [Opportunity].X120_After_RCD__c ”

    I tried the usual notation with curly braces like: {!Opportunity.X120_After_RCD__c}
    but got an invalid reference error and couldn’t save.

    TIA

    Brian

  10. Thanks, Rakesh! Question on Step 7 – why is the fourth field for “Set Object Variables” a picklist of fields, i.e “Forecast Category”, etc.?

    Best.
    Brooks

  11. Good one. I have some doubts around “one time activity” field. Can’t we just avoid it and have an evaluation criteria which says execute only if the stage is changed to “closed won”?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.