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 Force.com 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!

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS