Aug 19, 2024
86 Views
Comments Off on What is Clone and Deep Clone in Apex?
0 0

What is Clone and Deep Clone in Apex?

Written by

Clone is the predefined method in Apex which is used to clone the record just by one functional line of code.  

Deep Clone is the extended functionality of the Clone method which is used when we are required to clone the related list also within the record. To Deep Clone the record, you have to pass the isDeepClone parameter as true as shown below. 

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) 

In the above function, we have 4 parameters as preserveId , isDeepClone , preserveReadonlyTimestamps , preserveAutonumber 

  • PreserveId- It is used to preserve the ID of the record for the clone record. 
  • IsDeepClone- If true then Deep Cloning occurs in which related lists also get cloned. 
  • PreserveReadonlyTimestamps- It is used to preserve the read-only fields in the cloned record. 
  • PreserveAutonumber- It is used to preserve the auto number fields in a cloned record. 

What are the Differences between Deep Clone and Clone? 

There are certain criteria on which deep clone works and some limitations too with it which makes deep clone a different thing compared to clone. 

Here are some basic differences between clone and deep clone:  

Clone  Deep Clone 
Generally, it clones the list of objects and keeps the reference.  Generally, it clones the list of objects but doesn’t hold any reference. 
A Clone doesn’t keep the Ids of the related list as it simply clones the record.  A Deep Clone keeps the Id of related list . 
It supports primitive data types.  It doesn’t support primitive datatype. 
Parameters are not applicable.  Parameters are applicable. 

Here are some examples to give you a clear view of clone and deep clone differences:  

Let’s suppose, I am creating an Opportunity with some fields. 

Opportunity opp = new Opportunity (Name = ‘Salesforce Opportunity’, CloseDate = ‘08/20/2022’, StageName = ‘Prospecting’); 
insert opp;

Now, I am cloning it and inserting it again. 

Opportunity opp = new Opportunity (Name = ‘Salesforce Opportunity’, CloseDate = ‘08/20/2022’, StageName = ‘Prospecting’); 
insert opp; 
//Cloning the above Opportunity Record opp  
Opportunity = cloneOpp = opp.clone(false, false, false, false); 
insert cloneOpp;

It creates a new copy of the record with the queried fields filled with source record values. It generates a new record id as it keeps the reference.  

Now, when I try to deep clone the record, the deep clone is done by making the parameter isDeepClone true. 

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) 

Opportunity opp = new Opportunity (Name = ‘Salesforce Opportunity’, CloseDate = ‘08/20/2022’, StageName = ‘Prospecting’); 
insert opp; 
//Cloning the above Opportunity Record opp  
Opportunity = cloneOpp = opp.clone(false, true, false, false); 
insert cloneOpp;

If we make the first one parameter as true then it shows an error. 

Opportunity opp = new Opportunity (Name = ‘Salesforce Opportunity’, CloseDate = ‘08/20/2022’, StageName = ‘Prospecting’); 
insert opp; 
//Cloning the above Opportunity Record opp  
Opportunity = cloneOpp = opp.clone(true, true, false, false); 
insert cloneOpp;

This would give an error because Id is also considered with deep clone and cannot insert a deep cloned record. 

Article Categories:
Salesforce