Skip to content

A Apex Utility class to Clone a Salesforce SObject & and it's children.

License

Notifications You must be signed in to change notification settings

ChuckJonas/SObjectDeepClone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SObjectDeepClone

A Apex Utility class to Clone a Salesforce SObject & and it's children.

Install

  1. git clone
  2. cd into folder
  3. sfdx force:mdapi:deploy -d ./src -w 10000 -u [username]

Usage

1: Initialize SObjectDeepClone with:

  • Id of 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

  1. (Optional) make modifications to .clone

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

About

A Apex Utility class to Clone a Salesforce SObject & and it's children.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages