How to clone a record using Apex in Salesforce?

In this article we will learn how to clone a record using apex in Salesforce. In order to clone a record in Salesforce using apex we have to make use of clone() method of sObject class which allows developers to create a copy of an sObject. The clone() methods gives control to developer whether to retain record id, copy related records, preserve system generated fields and retain auto number fields.

What is the clone() Method in Salesforce?

The clone() method is part of the Apex sObject class and is used to create a new record based on an existing one. It returns a new instance of the sObject with the same field values as the original.

Have a look at syntax below.

sObject.clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

The below table will help you understand each parameters of the clone method.

ParameterTypeDescriptionDefault Value
preserveIdBooleanIf true, retains the original ID. If false, assigns a new ID.false
isDeepClone
Boolean
If true, clones related child records. If false, only the parent record is cloned.false
preserveReadonlyTimestamps
Boolean
If true, retains read-only timestamps (CreatedDate, LastModifiedDate).false
preserveAutonumber
Boolean
If true, keeps the original auto-number value. If false, generates a new one.false

Example 1: Basic Cloning Without ID Retention

Here’s a simple example where we clone an Account record without retaining the original ID:

Account originalAccount = [SELECT Id, Name FROM Account LIMIT 1];

// Clone without preserving ID
Account clonedAccount = originalAccount.clone(false, false, false, false);
clonedAccount.Name = originalAccount.Name + ' - Copy';
insert clonedAccount;

If you execute the above code a new account record will be created with a new ID however the related records are not cloned because the isDeepClone parameter is false and System-generated fields and auto-number values are reset.

Example 2: Deep Clone with Related Records

If you want to clone a record along with its related child records (deep clone), set isDeepClone to true.

Here’s an example of cloning an Account along with its Contacts:

Account originalAccount = [SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account LIMIT 1];

// Deep clone with related records
Account clonedAccount = originalAccount.clone(false, true, false, false);
insert clonedAccount;

// Cloning contacts related to the original account
for(Contact c : originalAccount.Contacts) {
    Contact clonedContact = c.clone(false, false, false, false);
    clonedContact.AccountId = clonedAccount.Id;
    insert clonedContact;
}

If you execute the above code a new account record will be created and related contact records are also cloned and assigned to the account.

Example 3: Preserve Read-Only Timestamps and Auto-Number

If you need to clone a record but retain the original creation date and auto-number value, set preserveReadonlyTimestamps and preserveAutonumber to true:

Case originalCase = [SELECT Id, CaseNumber, CreatedDate FROM Case LIMIT 1];

// Clone while preserving timestamps and auto-number
Case clonedCase = originalCase.clone(false, false, true, true);
insert clonedCase;

If you execute the above code a new case record will be created and the original createddate value in retained on new case record. However, Salesforce will not let you set CaseNumber directly — you’ll need to store it in a custom field if you need to reference it.

Example 4: Retain ID (Not Allowed for Insert)

If you try to preserve the ID and insert the record, it will fail because Salesforce does not allow inserting records with existing IDs.

This will result in an error:

Account originalAccount = [SELECT Id, Name FROM Account LIMIT 1];

// Trying to retain the ID (will fail on insert)
Account clonedAccount = originalAccount.clone(true, false, false, false);
insert clonedAccount; // This will throw an error!

When you execute the above code you will get below error.

The above code will work when you clear the ID before inserting. Have a look at code below.

Account originalAccount = [SELECT Id, Name FROM Account LIMIT 1];

// Trying to retain the ID (will fail on insert)
Account clonedAccount = originalAccount.clone(true, false, false, false);
clonedAccount.Id = null;
insert clonedAccount; // This will throw an error!

Use Cases for the clone() Method

The clone() method is useful in several scenarios:

  • Duplicate records for backups or testing.
  • Clone records with related child records for complex object hierarchies.
  • Preserve historical data by retaining timestamps and auto-numbers.
  • Automate record creation in batch jobs or data migrations.

Best Practices

  • Avoid cloning IDs unless necessary – Most scenarios don’t require ID retention.
  • Use deep clone only when needed – Cloning related records can increase processing time.
  • Preserve read-only fields selectively – Retaining timestamps or auto-numbers is helpful for audit trails but can confuse reporting.

Conclusion

The clone() method is a flexible and powerful tool in Salesforce Apex that allows developers to create copies of sObjects with precise control over how data is copied. By understanding the parameters and how they affect the cloning process, you can avoid common pitfalls and build more efficient Salesforce applications.

We hope now you have a better understanding of clone() method in Apex Salesforce. If you have any doubt then let us know.

Leave a Comment