Tag Archives: Clone

Difference Between Clone and DeepClone in Apex in Salesforce

Clone Deep Clone
Generally clone the list of object and keep the reference. Generally it clone the list of object but don’t hold any reference.
A Clone doesn’t keep the Ids. A Deep Clone keeps the Id.
Supports primitive data type. Doesn’t support primitive datatype.
Parameters are not applicable. Parameter are applicable.

Here is a an example to understand difference between Clone and DeepClone:
I am creating a New Account with some fields.

Account acc = new account(Name = 'Salesforce', Billingcity = 'San Francisco', Type = 'Customer - Direct', Phone = '9999999999');
insert acc;

This creates a record in the Account as shown below:

Now, I am cloning it and inserting again.

Account acc = new account(Name = 'Salesforce', Billingcity = 'San Francisco', Type = 'Customer - Direct', Phone = '9999999999');
insert acc;

//Cloning the above Account Record acc
Account accCloneCopy  = acc.clone(false, false, false, false);
insert accCloneCopy;

It creates a new copy of the record with same values, since it keeps the reference, new record ID is generated for cloned record.

Now, when I try to deepclone the record: (Deepclone is done by keeping true in the parameters) like,
clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

Account acc = new account(Name = 'Salesforce', Billingcity = 'San Francisco', Type = 'Customer - Direct', Phone = '9999999999');
insert acc;

//Deep cloning the above record
Account accDeepCloneCopy  = acc.clone(true, true, false, false);
insert accDeepCloneCopy;

It shows an error, because the Id is also considered and cannot insert the deppcloned record:

Custom Clone Button in Salesforce

Salesforce provides Clone functionality for some standard objects(Standard Clone button), However some standard objects do not have this button. For this purpose of cloning we will need to create custom button that will perform the functionality of cloning.

This cloning functionality can be achieved by writing a javascript for this custom button.

As an example lets create a custom button “Clone” on Account object that will clone the record.

Simply override your custom button “Clone” with the following javascript and you will have your custom Clone button that functions exactly like standard clone button

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
window.parent.location.href="/{!Account.Id}/e?&clone=1&retURL=/{!Account.Id}";

Note: retUrl specifies the location where you want to be on press of back button.

Clone a Record Using Apex in Salesforce

Salesforce sObjects have a method called clone() which creates a copy of the sObject record. This method has four Boolean type optional parameters.

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

Clone method parameters are as follows:

opt_preserve_id
Type: Boolean
Description: Determines whether the ID of the original object is preserved or cleared in the duplicate. If set to true, the ID is copied to the duplicate. The default is false, that is, the ID is cleared.

opt_IsDeepClone
Type: Boolean
Description: Determines whether the method creates a full copy of the sObject field, or just a reference:
If set to true, the method creates a full copy of the sObject. All fields on the sObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned sObject, the original sObject is not affected.
If set to false, the method performs a shallow copy of the sObject fields. All copied relationship fields reference the original sObjects. Consequently, if you make changes to a relationship field on the cloned sObject, the corresponding field on the original sObject is also affected, and vice-versa. The default is false.

opt_preserve_readonly_timestamps
Type: Boolean
Description: Determines whether the read-only timestamp fields are preserved or cleared in the duplicate. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate. The default is false, that is, the values are cleared.

opt_preserve_autonumber
Type: Boolean
Description: Determines whether auto number fields of the original object are preserved or cleared in the duplicate. If set to true, auto number fields are copied to the cloned object. The default is false, that is, auto number fields are cleared.

Example:

Account acc = new Account(Name = 'Salesforce', Billingcity = 'Texas', Phone = '9999999999');
insert acc;    

//Cloning the above Account Record acc
Account accCloneCopy  = acc.clone(false, false, false, false);
insert accCloneCopy;