Cloning objects in | Salesforce Developers Blog

I often receive requests from customer who have an object which they want to make a copy of, or clone, to support their business requirements. Just as often these objects have master-detail relationships, and need to have all relationship fields cloned as well. 

Thankfully, this is a very easy task to achieve within the platform by leveraging the clone method on the Apex sObject class, and set the optional opt_IsDeepClone argument to clone all relationship fields. Just remember that if you have triggers set up, cloning can cause unintended consequences that you must plan for in advance.

The sObject class provides a number of other useful methods that can make you life much easier so take a few minutes and review the documentation; There may be something you always needed, like the clone method, that you never realized was there!

Stay up to date with the latest news from the Salesforce Developers Blog