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.
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.
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.
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.
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.
6- Add Decision to Check The Result
Add a Decision element to check if there are contact roles related to the case.
7- Clone Contact Roles
If there are contact roles, you have to clone them. In order to do so, add a loop element.
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.
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.
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:
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:
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.
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.
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.