Salesforce | What’s Clone and Deep Clone in Apex?


Clone is the predefined technique in Apex which is used to clone the document simply by one purposeful line of code.  

Deep Clone is the prolonged performance of the Clone technique which is used after we are required to clone the associated checklist additionally throughout the document. To Deep Clone the document, you must cross the isDeepClone parameter as true as proven beneath. 

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) 

Within the above perform, now we have four parameters as preserveId , isDeepClone , preserveReadonlyTimestamps , preserveAutonumber 

  • PreserveId- It’s used to protect the ID of the document for the clone document. 
  • IsDeepClone- If true then Deep Cloning happens through which associated lists additionally get cloned. 
  • PreserveReadonlyTimestamps- It is used to protect the read-only fields within the cloned document. 
  • PreserveAutonumber- It is used to protect the auto quantity fields in a cloned document. 

What are the Variations between Deep Clone and Clone? 

There are particular standards on which deep clone works and a few limitations too with it which makes deep clone a special factor in comparison with clone. 

Listed here are some fundamental variations between clone and deep clone:  

Clone  Deep Clone 
Typically, it clones the checklist of objects and retains the reference.  Typically, it clones the checklist of objects however doesn’t maintain any reference. 
A Clone doesn’t hold the Ids of the associated checklist because it merely clones the document.  A Deep Clone retains the Id of associated checklist . 
It helps primitive information varieties.  It doesn’t help primitive datatype. 
Parameters are usually not relevant.  Parameters are relevant. 

Listed here are some examples to offer you a transparent view of clone and deep clone variations:  

Let’s suppose, I’m creating an Alternative with some fields. 

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

Now, I’m cloning it and inserting it once more. 

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

It creates a brand new copy of the document with the queried fields crammed with supply document values. It generates a brand new document id because it retains the reference.  

Now, when I attempt to deep clone the document, the deep clone is completed by making the parameter isDeepClone true. 

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) 

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

If we make the primary one parameter as true then it reveals an error. 

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

This may give an error because Id can be thought of with deep clone and can’t insert a deep cloned document. 





Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!