SObjectDeepClone
SObjectDeepClone copied to clipboard
A Apex Utility class to Clone a Salesforce SObject & and it's children.
SObjectDeepClone
A Apex Utility class to Clone a Salesforce SObject & and it's children.
Install
git clonecdinto foldersfdx force:mdapi:deploy -d ./src -w 10000 -u [username]
Usage
1: Initialize SObjectDeepClone with:
-
Idof the SObject you want to clone. For more control you can pass the SObject itself -
Set<String>of any child relationships you want to clone
-
(Optional) make modifications to
.clone -
Call
save(). Returns Id of cloned record
Example:
Id leadIdToClone = '00Q3A00001Q0wu7';
SObjectDeepClone cloner = new SObjectDeepClone(
leadIdToClone,
new Set<String>{
'Tasks',
'Events'
}
);
Lead beforeClone = (Lead) cloner.clone;
beforeClone.LastName = beforeClone.LastName + ' Copy';
Id clonedLeadId = cloner.save();
System.debug(clonedLeadId);
By default, all createable fields on the parent and target child relationships are cloned. If you need more control over what is cloned, you can instead pass in the actual SObject instance to clone (you're responsible for ensuring all data is present).
Considerations
- This utility is not currently optimized for cloning multiple objects (My use-case was to replace the Standard Layout
Clonebutton) - Currently limited to 5 relationships (due to SOQL query limit)
- Because we must run DML to properly test, you might need update
SObjectDeepCloneTestwith your own custom object generators to get tests to pass.