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 master flow:
Assume we have 2 records as follows, which we wish to clone. Note that the Payment Date is in the year 2019.
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.
Following is the master flow that we will be using:
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:
First, create a variable resource to capture the input record list that is passed from the master flow:
Next create another variable resource to collect the updated records, from which to create records:
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:It is important to note:
- The "Id" should be set to an empty string
- 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.
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:
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.
Hope you learned something today.
Cheers!
0 comments:
Post a Comment