Find your content:

Search form

You are here

Clone SObject Relationships


Is it possible to use a database query to get a set of related records and clone them into new related records.

sObject[] sObjectsWithChildRelationships = [SELECT Id, (SELECT Id, myObject__c FROM name_of_relationship__r) FROM myObject__c];
sObject[] cloneOfsObjectsWithChildRelationships = sObjectsWithChildRelationships.deepClone(false,false,false);

I'm trying to get the look-up relationship on name_of_relationship__r records to be duplicated into a new matching set of parent records, in this case myObject__c

Attribution to: jordan.baucke

Possible Suggestion/Solution #1

Why would going one by one mean running out of queries? You could grab them all, clone them into a list and update the list in groups of 200.

If doing that is still likely to present problems then I think the best way forward is for you to write a class implementing the Batchable interface, they're designed explicitly to do heavy data lifting like this!

Attribution to: Matt Lacey

Possible Suggestion/Solution #2

I wrote a generic utility class that can help with this. With your example, the usage of it would look something like this:

Id myObjIdToClone = '00Q3A00001Q0wu7'; //My_Object__c Id
SObjectDeepClone cloner = new SObjectDeepClone(
    new Set<String>{
Id clonedMyObjId =;

Note, it automatically copies all createable fields for the parent and children objects.

Attribution to: NSjonas

Possible Suggestion/Solution #3

Check out this newly posted package from the AppExchange: Astrea Clone. (Note: I have no connection with the publishers).

It doesn't help with this use-case in Apex, but allows the same task to be accomplished with point-and-click.

Attribution to: Benj

Possible Suggestion/Solution #4

I may not understand the questions, but I wrote a blog post a while ago about cloning records. See It may help. It basically has a method that will build you a SOQL query that contains all the writable fields. You'd need to make that query and then do a .clone() on the sObject in Apex and then make whatever changes you need, then insert it.

Attribution to: hemmeter

Possible Suggestion/Solution #5

The free Astrea Clone app only works with Standard Objects.

There is another App called Object Converter which can be used to clone Parent & Children for both standard and custom objects

Attribution to: bing

Possible Suggestion/Solution #6

Unfortunatly you cannot set in memory the relationship fields, so cannot reproduce the relationships in memory you can obtain by using inner queries for child or relatated records in SOQL, shame I agree.

What you might be interested in though is something I have recently developed and blogged about. It is a SObjectDataLoader class that has methods to serialise and deserialise SObject records including their related records. One of the use cases is to implement a deep clone (rather than the shallow one the platform provides). You can take a look at my blog article about it here.

Basically though it would allow you to do this...

Set<Id> myNewRecords = 
         SObjectDataLoader.serailise(new Set<Id>{ myExistingRecordId });

It uses JSON internally as the format, wrapped in an Apex custom class to maintain the relationships and insertion order of the serialised records. I know it actually inserts the records, so maybe not 100% what you want. Check out the full details and implementation approach on my blog article.

Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content