New Collection Action: DeepClone

It’s not very hard to clone a collection of records in Flow. You’ve been able to do it in Assignment node for a couple of years, and the Copy Collection action in Collection Processors is extremely efficient. A lot of cloning, however, requires that child/related records get handled as well. This new DeepClone action can really accelerate and simplify flow that do this kind of administrative work.

This action takes as inputs:

SObject inputRecord;You can provide either a record or a recordId. You have to provide one and only one.
String inputRecordId;You can provide either a record or a recordId. You have to provide one and only one.
List<String> childRelationships;Provide the names of the child relationships you want to clone. You can provide them either as a collection of Strings or a single comma-separated string. See ‘Specifying the Child Records’ below.
String childRelationshipsCSV;Provide the names of the child relationships you want to clone. You can provide them either as a collection of Strings or a single comma-separated string. See ‘Specifying the Child Records’ below.
Boolean saveImmediately = true;If set to true, the action will immediately save the cloned records. true by default. This needs to be true in order to clone child records because they need a parent recordId or you won’t be able to save them later.
Boolean saveChildRecordsAutomatically = false;Save the child clones immediately along with the parent clone.

Specifying the Child Records

Using either a List of Strings or a comma-separated string, provide the Child Relationship Name of the child records you want. For example, if you want to clone Opportunity Products along with an Opportunity, you would use ‘OpportunityLineItems’, as shown below:

How it Works

The action creates a clone of the provided input record and immediately saves it so it can obtain the new Id of the newly created clone record. The child records are then retrieved and cloned, and the new Id is used for their lookup value. The child records however are not immediately and automatically saved, as many use cases will call for them to be modified first. Use Create Records to save the child records and Update Records to save any changes to the cloned Parent record.

The action will attempt to append “Clone” and a date to the Name or Subject of the record being cloned so you can distinguish between clones in the event you don’t manually change the record name yourself.

Master-Detail Relationships

When working with master detail objects. Make sure to add ” __r” to the end of the relationship name. This is very important thing for custom fields and custom relations;

For example, you have a child object with a MD field that references Account. Then in Master Detail Options you can see that for the custom field, Child Relationship Name = Custom_Object_MD_Account. So in Deep Clone set childRelationshipsCSV = Custom_Object_MD_Account__r .

Install

Install via Collection Actions

View Source

Subscribe
Notify of
guest
20 Comments
Inline Feedbacks
View all comments
Andrey Shestakov

Hi! I glad I found this article!
Does specifying the child records work the same way for Master-Detail Relationship?
I used Child Relationship Name found found on my detail object, but have the error: Error Occurred: An Apex error occurred: SObjectDeepClone.DeepCloneException: Child Relationship ‘AREAS_OF_ASSESSMENT’ does not exsist on Assessment__c.
Assessment__c is Master object, and Area_Assessment__c is Detail object.

Andrey Shestakov

You’re right Alex. I tried the Action with another child object (Lookup field as well as on the video), and got the same error. Unfortunately, I have to much data in my sandbox, but if you can take a look on the snips attached – is there something with chosen settings?

Annotation 2020-05-31 172317.jpg
Andrey Shestakov

 

Annotation 2020-05-31 172317-2.jpg
Andrey Shestakov

 

Annotation 2020-05-31 172317-3.jpg
Daniel Neufeld

Hi Alex,
 
I’m trying to implement this in an org I can share. Any chance we could connect? Would love to hop on a quick screen share if possible!

Janardhana

Hi Andrey,
I have an exact scenario here, have got the same error. Doesn’t seem to work for Master-Detail Relationships.

Andrey Shestakov

Hi Janardhana, I checked with another child object, and got the same error, so I don’t think it’s related to Master-Detail. The cause is somewhere else…

Andrii

If you are working with custom fields, you need to make sure that you add __r to the name of the related list name, so for example I have a child object and MD field to account. Then In Master Detail Options is see that for my custom filed Child Relationship Name = Custom_Object_MD_Account. So in Deep Clone I will set childRelationshipsCSV = Custom_Object_MD_Account__r
 
I have just verified that and the deep clone component worked fine for me.

David

Thanks Andrii! Worked perfectly.

Vijay Aswani

I love the features of the Collection Objects, specially Deep Clone. Do you know if the cloned object can have a reference of the source object from which it was cloned. In your example, the Opportunity Product ‘Grog’ was cloned, and the clone was also stored as ‘Grog’. I would like to know the id of the source ‘Grog’ to be stored in the cloned object. I would need this because the to be cloned object has child objects which also need to be cloned, which also has child object which need to be cloned. So multi level child objects…… Read more »

[…] Clones a record and one or more sets of its child related records. More Information […]

Eyal Kama

Hi it appears from my test that lookup fields from orig parent are not copied to the cloned parent. Is that by design?

Don Schueler

Apex class AccountAssignmentSchedule: line 16, column 56: Invalid type: AccountAssignmentBatch2. Install Failed. Any thoughts? Summer ’20 Sandbox.
Looks like its an OLD class … but not sure I have every had another install of other pkgs care… Namespace? Maybe because unmanaged
 
 

Last edited 3 months ago by Don Schueler
Don Schueler

Looking for a good workaround to this. Deepclone is working GREAT except that in testing there are conditions where a long NAME in the Parent gets the “Clone / Datetime” Suffix tacked on the end without checking that it will overflow the size of the Name field (80) – Any Thoughts?

Ed m

DEEPCLONE (APEX): DeepCloneNewOpp
Inputs:
childRelationshipsCSV = OpportunityLineItems
inputRecordId = {!Id} (0061X00000BQyo7QAD)
saveChildRecordsAutomatically = true
saveImmediately = true
 
 
 
Error Occurred: An Apex error occurred: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Name
 
 
 
 

Last edited 3 months ago by Ed m
Kristian Mesa

Is there a way to not append the word clone on the cloned related list?

Ethan Granger

Say I’m cloning a closed Opportunity and I want to create a copy but as an open opportunity. How do i change the stage so that my clone is open?
(BTW, copying the record into a record variable, modifying the variable, and cloning the variable did not work).