How to clone records without manually assigning all the fields ~ Salesforce

Sachithra Dangalla
4 min readOct 31, 2020

--

Salesforce is an awesome platform to build sites with just a few clicks. I personally love the declarative programming which allow literally anyone (even non-programmers) to use visual components to facilitate complex use cases.

Declarative programming has come a long way since its humble beginnings, yet there are possible improvements that can make the life of the programmer easier. Record cloning is a common use case, which does not have its own element or component in visual flow nor process builder.

The popularly used method to clone a record is to create a record by assigning values from an existing record. If your record has a lot of fields (Salesforce allows up to 800 fields), manually assigning each and every field is going to be a tedious task. In addition, it is not going to be easily maintainable over time.

In this article, I will be discussing of a way to easily clone records using flows. The trick is to use subflows.

The main flow

Assume we have 2 records as follows, which we wish to clone. Note that the Payment Date is in the year 2019.

Results before cloning

Now assume that we have a use case to do the same payments this year. We can simply clone the records and update the Payment Date. In this example, I will be demonstrating cloning of multiple records and will be using loops for that. This approach is also valid for single records but you can choose to omit loops if you wish.

Create a flow using the following pattern, which will be used as the main flow. Ideally, the subflow has to be created first so that it is available to be called from the main flow, but for easier story line, I will be explaining the main flow first.

Main flow

The “Get Records” will collect the desired records (this is where you can specify any filters to collect only the desired records). The records obtained will be passed to the Subflow, which will be described next.

The Subflow

Now let’s create another flow that will be called by the main flow under the “Subflow” element. The following is the pattern used.

First, create a variable resource to capture the input record list that is passed from the master flow:

Variable to capture input

Next create another variable resource to collect the updated records, from which to create records:

Variable to capture the output

Next, we will loop through the records and assign variables to the looped variable.

Assignment-1

In the first “Assignment Element”, we will be setting the fields of the record as follows:

Assignment element 1

It is important to note:

  1. The “Id” should be set to an empty string
  2. The Payment Date is updated to match our use case

Assignment-2

In the second “Assignment Element”, we will be adding the record to a pre-defined list.

Assignment element 2

It is important to note:

You might wonder why we did not use a single assignment element to achieve both the above assignments. In salesforce flows, the updated fields are actually set after the flow completes execution of the element. Therefore, if we used a single assignment element to updated the fields AND add it to the list, the record added to the list is not the updated one, but the previous one itself.

Create records using the list

Once the records are looped and added to the list, use a “Create Records” element to create records using the list:

Create Records element

You can run the master flow and ensure that your records have been cloned without having to assign each and every field. Note that the Payment Date has been updated and all other fields are intact.

Results after cloning

Hope you learned something today.

Cheers!

--

--

Sachithra Dangalla
Sachithra Dangalla

Responses (1)