Friday, January 31, 2025
2.9 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

GAC AION DEBUTA NOVA VERSÃO GLOBAL AION V, TARGETING MERCADOS INTERNACIONAIS

AION, o novo braço de veículos de energia da...

Explore the Best Perfume in UAE for Every Budget

Have you ever noticed how a single spritz of...

How Does Google Ads Management Boost New Patient Lead For Dentists?

In today's competitive healthcare landscape, attracting new patients to...

Online Cricket ID Whatsapp Number: A Simple Way to Play Cricket 

Introduction to Online Cricket ID Whatsapp Number  Simply put, a whole lot...

Strategies for Managing Your Bankroll When Playing Live Casino Games

If you’re diving into the world of live casino...

Topics

Explore the Best Perfume in UAE for Every Budget

Have you ever noticed how a single spritz of...

How Does Google Ads Management Boost New Patient Lead For Dentists?

In today's competitive healthcare landscape, attracting new patients to...

Online Cricket ID Whatsapp Number: A Simple Way to Play Cricket 

Introduction to Online Cricket ID Whatsapp Number  Simply put, a whole lot...

Strategies for Managing Your Bankroll When Playing Live Casino Games

If you’re diving into the world of live casino...

Perfume Brands For Women That Scream Elegance Today

A signature scent is more than just a fragrance;...

Why Your Dental Practice Needs a Digital Marketing Agency

The dental industry is becoming increasingly competitive, with more...

The Rise of Avtube inside the Digital Landscape

In the hastily evolving virtual panorama, systems that seamlessly...

Related Articles

Popular Categories