top of page

How to clone records using Flows? One flow to clone them all! #Spring20Delight

Jan 15, 2020
3 min read

Hola! Good to see you here šŸ˜€

I am quite excited about this one. All the Salesforce Admins are going to love this one especially because this functionality has been asked so much.

Now, where to begin with. Wait, I have the perfect para to kick this one off! 😁

Safe harbor, stuff that you are going to see in this post is not GA yet. But soon you will be able to use it once the Spring 20 is released to production environments. I am using a pre-release org for the demo purpose.

So what’s new in the Spring 20 release that is going to contribute to this solution. We have a couple of new flow specific features contributing to this one:

1. You will be able to query all the fields for an record using the Get Records element, without adding/specifying individual fields. Refer the screenshot.

2. After Spring 20 release, we will be able to pass sObject(and collection) as parameters toĀ Invocable methods. So this enables us to write a generic reusable Apex Invocable method, which we will be able to use to clone any type of sObject record.

Wait, something doesn’t feel right!

I know what you might be thinking. Why the need to create an Apex action when we can already query all the fields? Why not simply copy the record variable to another record variable of same type using an Assignment element, set the Id field to NULL of the record variable and use Create Records element to create the record.

Well, I had a very similar thought too. But when I tried to do the same, it didn’t work. So it seems that it does query all the fields because you can reference any field(tried and tested), but if you copy the record variable, it will only just copy the ā€˜Id’ field. A bit weird, but that’s how it’s built I guess. Hence the need for an Apex action.

How does the Apex action look?

CloneRecordFlowActionClass.apxc

public class CloneRecordFlowActionClass { @InvocableMethod(label='Clone Record') public static FlowOutputParameters[] cloneRecord(FlowInputParameters[] input){ FlowOutputParameters[] result = new FlowOutputParameters[]{}; sObject s = input[0].recordToClone; // Copying the input record to an sObject type variable s.put('Id',NULL); //Setting the 'Id' field of the record as NULL insert s; //Inserting the sObject record FlowOutputParameters obj = new FlowOutputParameters(); obj.clonedRecordId = (string)s.get('Id'); //Copying the inserted sObject record Id to return as an flow output result.add(obj); return result; } /* Input parameters for the Apex action */ public class FlowInputParameters{ @InvocableVariable(label='Record to Clone' required = true ) public sObject recordToClone; } /* Output parameters of the Apex action */ public class FlowOutputParameters{ @InvocableVariable(label='Cloned Record ID') public string clonedRecordId; }}

Input-Output of Apex action

Input(s): -Record to Clone: Pass the variable(of ā€˜record’ type) that you want to clone. Output(s): -Cloned Record ID: Use a text variable to store the ID of the cloned record.

Demo!

Things to know!

Instead of returning the clone record as a record variable back to the flow, I decided to insert the record within the apex action and return the ID of the cloned record. Why? Because the advantage of doing this is admins/devs can just create one flow to handle cloning of records of all the objects(standard and custom, both). Otherwise, you will have to create multiple flows each specific to one object to clone the records.

Update[ March, 2020]

There is a super efficient way to do this declaratively without using Apex. Refer this article:Ā https://admin.salesforce.com/blog/2020/duplicate-a-record-with-lightning-flow-in-spring-20

So no need to write apex! šŸ˜€

Hope you find it useful!

Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. šŸ™‚

Ā 
Ā 
Ā 

Comments


bottom of page