How to Update the Triggering Record in Before Save Record Triggered Flow

How to Update the Triggering Record in Before Save Record Triggered Flow

Record triggered flow can be considered the most common flow type in Salesforce. It is the only flow type that automatically runs when a record is created, updated, or deleted. Depending on what you want to perform, you can choose to run the flow before or after the record is saved. If you go for "Fast Field Updates", it runs before save, while "Actions and Related Records" runs after save. If you just need to update fields on the record that triggers the flow, before save is the right option. Although you can update only the triggering record, it is crucial to use before save instead of after save. Because before save flow runs 10 times faster than the after save flow! Moreover, it doesn't consume any DML limits.

There are 2 ways to update the triggering record in before save record triggered flow.

Using the Assignment Element to Update the Triggering Record

The concept of "before save" implies that changes occur prior to the record being saved to the database. As a result, there is no actual update; it simply modifies the field values before saving. This is also the reason why a before save record triggered flow doesn't consume any DML limits.

When Salesforce introduced the before save record triggered flow, Assignment element was the only option to update the triggering record. To update the triggering record, you only need to include an Assignment element and configure the desired field values. No additional Update Records element is required.

Assignment Element to Update Record in Before Save Record Triggered Flow

For instance, updating the Opportunity record requires just these simple steps.

Update Opportunity Using the Assignment Element

After a short time, Salesforce added another option to update the triggering record: Update Records element. Let's see how to use the Update Records element in before save record triggered flow.

Using the Update Records Element to Update the Triggering Record

In other types of flows, it is possible to use the Update Records element to update any record in the system. However, Update Records element can update only the triggering record in before save record triggered flow. As you can see, the other options are not available.

Using the Update Records Element
Using the Update Records Element to Update the Triggering Record

We mentioned that the concept of "before save" implies no record update. So, what purpose does the Update Records element serve? In a before save record triggered flow, the Update Records element functions similarly to the Assignment element, meaning it doesn't consume from DML limits either.

Here are the debug logs of this before save record triggered flow with an Update Records element. As you can see, it doesn't consume from DML statements/rows.

Debug logs of a before save record triggered flow with an Update Records element

Both options update the triggering record, and neither consumes from the DML limits. So, what sets them apart? Which one should you choose?

Differences Between Assignment and Update Records Elements in Before Save Record Triggered Flow

As you can see from the previous examples, there is no big difference between these elements in before save record triggered flow. There is no definitive correct option; you can use either of them to update the triggering record. Now, let's explore the pros and cons of each element.

  • You can use the Assignment element to assign values to other variables too. Therefore, it can be hard to see which Assignment element is updating the record and which one is not. Using the Update Records element can improve the visibility and provide a clearer indication for the user.
  • If you decide to switch the before save flow to an after save flow, your Assignment elements will no longer update the record. On the other hand, the Update Records element will persist in updating the triggering record. This flexibility enables you to seamlessly transition between record triggered flows that run before or after the record is saved. However, if you built your flow using multiple Update Records elements, you may need to rebuild your flow after switching to after save. This is because it is not a good practice to update the same record multiple times in an after save record triggered flow.
  • While both perform the same function, there is a slight difference in their performance. Both execute very quickly, that's for sure. However, based on some tests, the Assignment element runs a little bit faster than the Update Records element.
Debug Logs for the Assignment Element

Here are the results of a 10 round test run for these elements.

AssignmentUpdate Records
1st Run0.25 seconds0.25 seconds
2nd Run0.27 seconds0.34 seconds
3rd Run0.21 seconds0.24 seconds
4th Run0.21 seconds0.29 seconds
5th Run0.23 seconds0.40 seconds
6th Run0.21 seconds0.36 seconds
7th Run0.23 seconds0.25 seconds
8th Run0.26 seconds0.28 seconds
9th Run0.24 seconds0.26 seconds
10th Run0.24 seconds0.24 seconds
Average0.24 seconds0.29 seconds

Summary

Both the Assignment and Update Records elements can modify the triggering record in a before save record triggered flow. Furthermore, neither of them consumes from the DML limits. Although they both have some pros and cons, there is no absolute right or wrong approach to updating the triggering record.

Assignment and Update Records Elements in Before Save Record Triggered Flow

3 Comments

Leave a Reply

Your email address will not be published.


*