Using Flow to Clone a Record With Its Related Records

Using Flow to Clone a Record With Its Related Records

Cloning a record means making a copy of a similar record. In order to clone a record in Salesforce, it is possible to use the standard button called Clone. It is useful for many use cases and it saves time. For example, you can use it to renew a contract or create recurring records. Even though this button clones the record, it does not copy the fields that are not on the page layout.

In some cases, it is not enough to clone only the main record and you might want to clone its related records as well. There is another standard button called Clone with Related, which can be used for this purpose. However, this button has many limitations. First of all, it is not available in the Salesforce mobile app. Secondly, and most importantly, it is available only for opportunity and campaign objects. What if you want to clone another object with its related records? It is possible to achieve this with Salesforce Flow.

Read this article to learn more about the standard Clone and Clone with Related buttons and their limitations.

Clone with related records button on opportunity

Create a Flow to Clone a Record with Its Related Records

Depending on the use case, different flow types can be used to clone the records. If you want to display a screen to the user and ask which related records to clone, then you have to build a screen flow. If you go with a screen flow, you can also display some of the fields and ask for new values. For example, if you are cloning an account record, it doesn't make sense to create a new account record with the same name. Therefore, you can display a text field on the screen and ask for the new account's name. If you want to automate the process and clone the record when it is updated to meet a criteria, then you have to build a record-triggered flow. In this case, since there cannot be a screen, you cannot ask the user to select which related records to clone. Therefore, from the beginning you have to decide which related records to clone. Don't forget to make it an after save flow, otherwise you cannot even create records. It is possible to use other flow types as well, but screen flow and record-triggered flow are the best options for this purpose.

Read this post to learn more about the flow types.

Screen Flow to Clone a Case Record with Its Related Records

Let's create a screen flow to clone a case record with its related records. In this example, related records will be contact roles, case team, and files. It is a very good example of working with collections and using the loop element in Salesforce Flow. If you don't know how to work with collections, I recommend you to read this post first.

1- Create a Screen Flow

Create a new flow and select screen flow as the flow type. Then create a record variable called recordId. As you can see in this post, you can create it as a record variable instead of a text variable. Mark it as available for input. By doing this, action button will be able to pass the entire case record to the flow.

2- Create the Main Screen

Add a screen element to the canvas and add some fields that you want to display. For example, you can display the subject, description, and case origin fields. These are the fields that the user can change, all the remaining fields will be cloned as they are. Use the current field values as the default values for these fields, so that if the user doesn't want to change them, they can stay as they are.

Optionally, add a section to the screen to build it as two columns. Add a display text and three checkbox fields to the right column. Users will be able to select which related records to clone.

Screen to clone records
3- Create the New Case Record

Add Create Records element to create a new case record. Select all the fields that you want to clone but don't forget to map the screen input components to the right fields. On the other hand, don't map the fields that don't make sense. For example, don't map the Status field. Instead, set it as New.

New case record mapping.

After this step, the case record is cloned. However, you have to clone the related records that the user selected.

4- Check Which Related Records to Clone

Since the user can decide which related records to clone, you have to add a decision element to understand which records to clone. In order to do this, add a Decision element to the canvas and check the values of the checkbox fields.

Decision element to check the values of the checkbox fields
5- Clone the Contact Roles

Add a Get Records element to get the case contact roles related to the case. Don't forget to mark the checkbox to get all the records and not only the first one.

Get case contact roles.
6- Add Decision to Check The Result

Add a Decision element to check if there are contact roles related to the case.

Decision element to check the result of the get records.
7- Clone Contact Roles

If there are contact roles, you have to clone them. In order to do so, add a loop element.

Loop through contact roles.

Then, create a record variable called ContactRole and add an Assignment element to set its values. ContactId and Role fields should be equal to the values of the current item from the loop. Use the Id of the new case record for the CaseId field.

Assign values to contact role to clone records.

Add another Assignment element to add the ContactRole to a new record collection called ContactRoleCollection (create a new variable for this). Connect this new Assignment element back to the loop.

At the end of the loop, create CaseContactRole records at once.

Clone contact role records.
8- Clone the Other Records

Repeat the same steps to clone the Case Team and Files. After each decision, if there is no need to clone, move to the next decision element that checks the next checkbox value. Likewise, after each record creation, move to the next decision element that checks the next checkbox value.

To clone the Files, use the ContentDocumentLink object. Use the new case Id for the LinkedEntityId field.
To clone the Case Team, use the CaseTeamMember object. Use the new case Id for the ParentId field.

9- Show Toast Message

After the records are cloned, display a success message to the user. You can display a message on a flow screen but I recommend you to display a toast message. In order to do so, install the ShowToast flow action from Unofficialsf. Add an Action element to the canvas, select showToast and configure it like this:

Show toast message.
10- Navigate to the New Case

When you clone a record using the standard Clone button, it takes you to the new record. In order to do the same behavior, I recommend you to install the NavigateEverywhere flow action from Unofficialsf.

After installing this package, add an Action element to the canvas and select navigateEverywhereLFA. Configure the action like this:

Navigate to the new case.
11- Activate the Flow

This is the end of the flow. Depending on the related records that you want to clone, your flow should look like this. After you are done with the flow, don't forget to activate it.

End of the flow to clone records.
12- Create an Action to Run the Flow

Create a new action on the Case object. Select Flow as the Action Type and select your flow from the dropdown list. After saving the new action, add it to the page layout.

Action on the case object to clone records.

This is one of the few ways to run the screen flow. You can create a button, put the flow as a component on the Lightning page, or use other custom solutions as well.

Action Time

17 Comments

  1. Hi, Thanks for the information. However I followed this to clone cases with Files and I'm not able to clone the Cases with Attachments. Would you please help me on this.

    • Hi Ayush,
      In that case you have to create an attachment record with the values from the existing attachment (no content document or content document link records).
      Just enter the new case's Id to the ParentId field of the attachment.

  2. Hi, this tutorial help me a lot, thanks! One question, It is possible to use record variable and use a button to launch the flow? Or is needed to use text variable and then get the Id?

  3. Hi Yumi and thanks for all you do
    I experienced an issue using a flow to clone accounts in an org with person accounts enabled
    I assigned the account single variable to the old account and used this in a create records element
    However I got an error INVALID FIELD INSERT UPDATE
    Looking at all the fields,it was caused by fields with __pc which are from the person accounts
    Any ideas?

    • Hi,
      Some fields are read only for person accounts. For example, you cannot set the name. You have to use FirstName and LastName fields for person accounts.
      So the error might be related to this.

  4. Hi,

    Using flows, can we clone multiple parents and multiple child records?
    Example: Parent Records: Chocolate Ice Cream, Vanilla Ice Cream
    Child Records: 2 Chocolate Ice Cream, 3 Vanilla Ice Cream.

    I want to give the users option to create same order again..NOTE: Above is just an example, it is not the actual scenario..

    I am stuck with How to identify correct parent and child record after clone, I am cloning Parent separately and children separately, to avoid DML within the loop... but now I am stuck as I dont know how to map newly created records correctly.

  5. Hi Yumi,

    Really enjoyed taking your flow course recently on Udemy!

    I would like to use this concept to Get the Related Products (Price Book Entities) of a Price Book. However, I am stuck on the Conditions for the Get Related Products. I've select Product as the object, but can't figure out the Condition. All Conditions are Met (AND) For the field I assume it is Id (Product ID)? What is the value?

    Thanks!

    Michael

    • Hi Michael, thank you!
      Do you want to get products from the Price Book? The object is called PriceBookEntry so you need to get PriceBookEntry records that are related to that PriceBook (PriceBookId). This will get the PriceBookEntry records, is it what you need or do you want to get the Product records?

      • Hi Yumi,

        Making good progress on the flow. Everything seems to be working fine, but I am getting the following error on create: "FIELD_INTEGRITY_EXCEPTION: Price Book ID: id value of incorrect type: 01t0y000005XiEHAA0. You can look up ExceptionCode values in the SOAP API Developer Guide.---This error occurred when the flow tried to create records:"

        In Assignments, my value is {!Create_Price_Book} and my variable is {!PriceBookEntryVariable.Id}.

        Is that where the error is? Bit confused...

  6. Hi Yumi,

    Making good progress on the flow. Everything seems to be working fine, but I am getting the following error on create: "FIELD_INTEGRITY_EXCEPTION: Price Book ID: id value of incorrect type: 01t0y000005XiEHAA0. You can look up ExceptionCode values in the SOAP API Developer Guide.---This error occurred when the flow tried to create records:"

    In Assignments, my value is {!Create_Price_Book} and my variable is {!PriceBookEntryVariable.Id}.

    Is that where the error is? Bit confused...

  7. Hi Yumi,
    Since my screen flow is cloning a price book rather than a case, what do you recommend using instead of creating an action to run the flow from a price book record?

    Thanks,

    Michael

  8. This flow worked perfectly when debugging -- however it will not work from a related list on parent object. The requirement is for the user to check 1 record on a related list on parent record, then clone the selected record AND its related records. Do you have any suggestions?

3 Trackbacks / Pingbacks

  1. Launch Screen Flow in Modal for a Record in a Related List - Salesforce Time
  2. How to Launch a Flow from the REST API - Salesforce Time
  3. How to Apply Case Assignment Rules in Flow - Salesforce Time

Leave a Reply

Your email address will not be published.


*