Salesforce recently updated Flows so that before-save flows can now have “Update Records” as an option to update fields on an inserted or updated record before it’s saved to the database. Originally, one had to do it through the Assignment element but now either is an option. Let’s benchmark each and see if one performs better than the other.
Averages After 10 Runs
|Before-Save Assignment||Before-Save Update Records|
|61.1 milliseconds||64.8 milliseconds|
See Record Automation Benchmarking for additional timings of the different same-record update options.
For each solution, a custom object was created with an Auto Number name field and with a custom text field. 200 records are inserted in each solution’s custom object using apex from Execute Anonymous using VS Code and each solution sets the custom text field to the same value. Immediately before and after the insert statement Limits.getCPUTime is invoked. The start time in milliseconds is subtracted from the end time to determine the overall insertion time. These results are then recorded in a Benchmark Result custom object.
A before-save flow was created on each of these two objects. One flow uses the “Assignment” element to update the “Text 1” field to “Benchmark Test”. The other before-save flow does the same thing but with an “Update Records” element.
System.debug was avoided because turning on system.debug usually causes everything to be slower. Dan Appleman and Robert Watson shared that in their Dark Art of CPU Benchmarking presentation. Parsing each debug log after each benchmark run is also tedious.
The timings shown include the general overhead of inserting 200 records. As a result, the timings shouldn’t be interpreted as each operation takes X time. Each one is X plus Y overhead.
After 10 runs each, the Assignment element averaged 61.1 milliseconds versus the Update Records’ 64.8 milliseconds. This indicates that their runtime performance is approximately equal. It’s unclear why Salesforce added another element to Before-Save flows that essentially does the same thing. My guess is to reveal the intent of the flow at first glance in the canvas without knowing that Assignments do the same thing.
Update Records also let’s one specify the criteria under which the records should be updated, which the Assignment operator doesn’t have. One would have to use a decision element first so that’s nice that it saves a step but my preference is to have the decision element so that one can see it’s being “filtered” and updated only if some criteria is met. Update Records is also slightly faster at defining the field definitions because the fields are shown immediately whereas one has to choose $Record and then the field in an assignment.
I don’t have a strong preference over using one over the other as long as the same way of doing it is used consistently throughout the org. It would be nice if there’s just one way of doing it so builders don’t have to constantly answer the question “Which should I use and why?”. If you have a compelling reason to use one over the other, let me know in the comments!
Before-Save Assignment Timings
|Run #||Time in Milliseconds|
Average: 61.1 milliseconds
Before-Save Update Records Timings
|Run #||Time in Milliseconds|
Average: 64.8 milliseconds