How to Clone the Record With Related List in Salesforce : Abhijeet

How to Clone the Record With Related List in Salesforce
by: Abhijeet
blow post content copied from  SalesForce FAQs
click here to view original post



### Summary of Cloning Records with Related Lists in Salesforce In Salesforce, you can clone records, but there are specific limitations based on the type of record and its relationships. Here’s a simplified breakdown: 1. **Cloning Options**: - **Clone Quick Action**: This allows you to clone a specific object record, but it does not include child records (like Opportunity Products or Quotes). - **Deep Clone**: Available for the Accounts object, this option allows cloning of related records, but it is not available for other objects. 2. **Cloning Child Records**: - To clone child records (related lists), you can use the **"Clone with Related List"** feature, but it only works for standard objects like Opportunity and Campaign. - For custom objects or complex relationships, you need to customize the process using **Apex triggers** and **Flows**. 3. **Cloning Process**: - **Using Standard Button**: - Navigate to the object settings (e.g., Opportunity), add the "Clone with Related" button to the page layout, and use it to select and clone related records. - **Using Apex Code**: - For more complex cloning, create a custom Apex class that defines the logic to clone records and their related records. This involves querying the original record, cloning it, and then cloning its related records. 4. **Visualforce Page**: - To facilitate user interaction, you can create a Visualforce page where users can input the ID of the record they want to clone. This page will call the Apex class to perform the cloning operation. 5. **Conclusion**: - Cloning records with related lists is straightforward for standard objects but requires custom solutions for complex scenarios. By using Apex and Visualforce, you can effectively clone records along with their related child records. ### Additional Context - **Apex**: A programming language used in Salesforce for custom business logic. - **Visualforce**: A framework for building custom user interfaces in Salesforce. - **Related Lists**: These are child records associated with a parent record, displayed on the record detail page. ### Hashtags for SEO #Salesforce #CloneRecords #ApexDevelopment #Visualforce #SalesforceCustomization #CRM #SalesforceTips #SalesforceTraining #SalesforceDevelopment #SalesforceAdmin


To create similar records in Salesforce, we have an option to clone that specific object record using the Clone quick action. But it does not clone child records in a master-detail relationship, such as an Opportunity that has Opportunity Products and Quotes.

In the Accounts object, you will get the option to clone the related records using the “Deep clone” button, but it is not available in other objects.

Let’s see what process we need to follow to enable the settings that allow us to clone the record with the related list in Salesforce.

Clone the Record With Related List in Salesforce

Related list records are the child records connected to a parent record and shown in the Related Lists section on a record’s detail page. To clone child records, we have the “Clone with related List” feature, but it only supports standard objects like Opportunity and Campaign.

For custom objects or complex relationships, we require customization using Apex triggers and Flows to clone records along with their related records.

You can check out this solution to clone the related records using flow in Salesforce.

In the examples below, we will see the methods to clone records with related list records. The first approach is using the standard button in the user interface, and the other is programmatically using Apex code.

  • Clone Record With Related List Using Standard Button in Salesforce
  • Clone Record With Related List Using Apex Code

Clone Record With Related List Using Standard Button in Salesforce

For the standard Salesforce object, we can clone the related records using the standard button Clone with Related.

Let’s see how to add the standard Clone with Related button to the record page to clone the record with the related child records.

  1. Navigate to Setup > Object Manager > select object (Opportunity), then select Page Layouts in the side bar.
How to clone recods wirth related lists in Salesforce
  1. In the page layout setup, click on the Mobile & Lightning Actions tab. Select the action Clone with Related” and drag it to the section Salesforce Mobile and Lightning Experience Actions.

After this, click save to apply the changes.

Clone Opportuniites with related records
  1. On the record page, you will see the button Clone with Related in the quick actions. As we click on it, a pop-up window, Clone with related records, will appear.

In this, we can select the related records to clone with the current records by activating the checkboxes.

After selecting the related records, click Next.

Clone Related list in Salesforce

In the next window, edit the details of the records along with the details of related records and save the changes.

With this, the current record, along with the related records that you have selected in the Clone with Related quick action.

Clone Record With Related List in Salesforce Using Apex Code

To handle complex use cases that require cloning related records beyond what the standard user interface allows, we can create a custom Apex class.

We will create an Apex class that clones Account records along with their related contacts. To call this method, we will create a visualforce page. On the Visualforce page, the user can enter the account ID and clone it along with its related contacts.

Now, follow the steps below to clone the records with related lists in Salesforce using Apex.

  1. First, we will create the apex class, in which we will define the logic to clone the record. To create an apex class, navigate to the Salesforce developer console and select File> New> Apex Class.
Create an Apex class to clone related records in Salesforce
  1. Enter the label for the Apex class, such as CloneRelated, and click OK.
  2. Enter the code below in the Apex class and save it.
public class CloneRelated {

    public String idOfRec {get; set;}
    public CloneRelated() {
    }

    public void cloneRec() {
        List<Contact> cons = new List<Contact>();
        Account acc = [SELECT ID, Name FROM Account WHERE Id = :idOfRec];
        Account accCopy = acc.clone(false, true);
        insert accCopy;

        List<Contact> con = [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = :acc.Id];
        for (Contact c : con) {
            Contact conCopy = c.clone(false, true);
            conCopy.AccountId = accCopy.Id;
            cons.add(conCopy);
        }

        insert cons;
    }
}

In this apex class, we have defined a property idOfRec to store the ID of the Account to be cloned.

Then, the cloneRec() method first queries the original Account using this ID, then creates a clone of it using the clone(false, true) method.

After inserting the cloned Account, it retrieves all Contacts linked to the original Account, clones each one, updates their AccountId to point to the new Account, and inserts them as well.

Creating the Visualforce Page For Calling Apex to Clone Records

To call the apex class method, we will create a Visualforce page form where the user can enter the ID of a specific account record and clone it along with its related contacts.

  1. Navigate to Setup>Visualforce Pages and click the New button to create a Visualforce Page.
  2. Enter the Label for the Visualforce page and, in the code editor, enter the code below to call the Apex class method.
<apex:page controller="CloneRelated" sidebar="false">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection >
                <apex:pageblockSectionItem >Enter the Account Id:</apex:pageblockSectionItem>
                <apex:pageblockSectionItem >
                    <apex:inputText value="{!idOfRec}" />
                </apex:pageblockSectionItem>
            </apex:pageblockSection>

            <apex:pageblockButtons >
                <apex:commandButton value="Clone" action="{!cloneRec}"/>
            </apex:pageblockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>

Save the visualforce page, and we can also view the form UI through the Preview button. The preview of the visual page form will look as shown below.

Clone Related list in Salesforce with Apex

To make this form visible to the users, we need to deploy it on the lighting page. For that, go to the Lightning page (app page, record page, or home page) and click on the settings icon and select Edit page.

  1. In the Lightning App Builder, navigate to the Components tab, then drag and drop a Visualforce component into the page region.
Clone Related records using Apex Class
  1. By default, it will display any random visualforce page. We can select the visualforce page from the Visualforce Page Name lookup field in the right-side bar. In this, we can also define the label and height of the component.

After this, click Save to apply the changes in the lightning page.

Create an Apex class to clone related records

Now, to clone a record, users have to enter the Account ID in the input field and press the Clone button.

Use apex to clone a record with related list in Salesforce

To get the account ID, navigate to the account record that you have to clone. In the URL of the record, a 15-digit code will be displayed, which is the account record’s ID.

Clone with related records using record id in Salesforce

Copy the record ID and paste it in the input of the visualforce form, then click on the Clone button.

Clone Account with related list using Apex in Salesforce

After this, go to the account object and set the list view to New this week. There you will see the cloned Account record with its related contacts.

Clone Related records using Apex in Salesforce

This way, we can clone a record with its related list in Salesforce by using the Apex code.

Conclusion

Cloning records with related lists in Salesforce is easy for standard objects using the “Clone with Related” button. However, for custom objects or more complex logic, this option isn’t available.

In such cases, you can use Apex code and a Visualforce page to build a custom UI solution. This lets you clone a record and all its related child records, like cloning an Account along with its Contacts.

You may also like to read:

The post How to Clone the Record With Related List in Salesforce appeared first on SalesForce FAQs.


April 29, 2025 at 03:52PM
Click here for more details...

=============================
The original post is available in SalesForce FAQs by Abhijeet
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce