Activity Forums Salesforce® Discussions What is the difference between Clone() and DeepClone() in Salesforce Apex?

  • vishnu

    Member
    September 14, 2016 at 7:46 am

    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.

  • PRANAV

    Member
    January 19, 2018 at 1:14 pm

    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.

  • Adarsh

    Member
    March 30, 2018 at 10:14 am

    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.

  • Archit

    Member
    March 30, 2018 at 10:39 am

    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.

Popular Salesforce Blogs