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.Â