Activity › Forums › Salesforce® Discussions › What is the difference between Clone() and DeepClone() in Salesforce Apex?
-
What is the difference between Clone() and DeepClone() in Salesforce Apex?
Posted by Tanu on September 13, 2016 at 1:42 PMHii All,
I want to know the difference between between Clone() and DeepClone() in Apex.When should we use what? Can someone explain with an example?
Archit replied 8 years, 1 month ago 5 Members · 4 Replies -
4 Replies
-
Hi tanu,
If a list is cloned, it duplicates it and has reference.
If a list is DeepCloned, it duplicates and doesn’t have any reference. - [adinserter block='9']
-
Hi Tanu,
CLONE
- Creates a copy of the sObject record and keep the reference.
- Supports primitive data type.
- Parameters are not applicable.
DEEPCLONE
- Generally it clone the list of object but don’t hold any reference.
- It doesn’t support primitive datatype.
- Parameter are applicable.
Hope this helps you.
-
Hi,
Clone(): When you clone an sObject in Apex, it copies all the fields populated in that Apex object, not necessarily all fields on the record. … If you do the following, the clone will not have LeadSource and Status cloned from the original record. It will use the default values for Leads.
DEEPCLONE(): If a list is DeepCloned, it duplicates and doesn’t have any reference.
Parameters of DEEPCLONE:
Boolean opt_preserve_id – Whether cloned sObjects records ids are maintained.Boolean opt_preserve_readonly_timestamps– Whether cloned sObjects records read only system fields like createdDate, LastModifiedDtate, etc are maintained.
Boolean opt_preserve_autonumbe– Whether cloned sObjects records auto number fields are maintained.
-
Clone : means creating a new record with the existing details of another reord.
Here is the example:
Account acc = [SELECT Name, Type FROM Account LIMIT 1]; Account accCopy = acc.clone(false, false, false, false);
if you insert accCopy, it will be the exact copy of acc.
DeepClone : creating a new record with the existing details of another record along with related lists.
Log In to reply.