Sunday, December 15, 2024
11.6 C
London

What is Clone and Deep Clone in Apex?

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. 

Hot this week

Essential Hoodie Canadian Personal Style and Expression

Essential Hoodie: Canadian Personal Style and Expression In Canada, the...

Essential Hoodie Personal Style and Expression

Essential Hoodie: Personal Style and Expression The essential hoodie, once...

Key Questions to Ask Garage Builders Before Starting Your Project

Discover essential questions to ask garage builders before starting your project to ensure quality and avoid surprises.

What’s Next for AP Dhillon After Brown Munde?

AP Dhillon’s breakthrough single, Brown Munde, has undoubtedly changed...

Share the bliss of relaxation with mutual massage services in London

In London, where fast-paced lifestyles and daily stresses take...

Topics

Essential Hoodie Canadian Personal Style and Expression

Essential Hoodie: Canadian Personal Style and Expression In Canada, the...

Essential Hoodie Personal Style and Expression

Essential Hoodie: Personal Style and Expression The essential hoodie, once...

Key Questions to Ask Garage Builders Before Starting Your Project

Discover essential questions to ask garage builders before starting your project to ensure quality and avoid surprises.

What’s Next for AP Dhillon After Brown Munde?

AP Dhillon’s breakthrough single, Brown Munde, has undoubtedly changed...

Share the bliss of relaxation with mutual massage services in London

In London, where fast-paced lifestyles and daily stresses take...

Glo Gang The Rise of a Powerful Hip-Hop Collective

Glo Gang, short for "Glow Gang," is a prominent...

Syna World Hoodie A Trendy Fashion Statement for Comfort and Style

In the ever-evolving world of fashion, hoodies have become...

Forex Trading Websites: A Comprehensive Guide to Online Trading

Forex trading has grown immensely in popularity, thanks to...

Related Articles

Popular Categories