How to Roll Back Records in Screen Flow

How to Roll Back Records in Screen Flow

Salesforce introduced a brand new element called Roll Back Records in the Winter '22 Release. Roll Back Records data element is available only in screen flows. You can use it to roll back a transaction and cancel all its pending record changes.

Data elements of Screen Flow

What is Flow Transaction?

Transaction is a group of operations that are executed as a single unit. For instance, a transaction can execute multiple DML operations. If one of these operations in the same transaction fails, all of the modifications in the transaction are rolled back.

Flow transactions end when a screen element, local action or pause element is executed. Since there is no pause element in screen flows, screen element or local action can be used to end the transaction.

Roll Back Records Element

When a screen flow executes the Roll Back Records element, it rolls back the transaction and cancels all its pending record changes. You can connect a fault path to this element in order to roll back the changes. By doing so, you can prevent having missing data in the database. However, it is important to note that the actions that were done before the current transaction won't be rolled back.

How to Use the Roll Back Records in a Fault Path

Here is a screen flow that creates an account and a contact under it. However, there is a validation rule that blocks creating the contact.

1- If you don't handle the errors, the flow will fail while creating the contact record. The user will see the standard error message and flow will roll back the transaction. Therefore, both account and contact records will not be created.

Screen flow to create an account and contact.

Here is the result. The user will see this standard error message and will not understand what happened. On the other hand, flow will roll back the transaction so that none of the records will be created.

Result if you don't handle the errors

2- It is the best practice to handle the errors in screen flows. If you handle the errors, the flow will still fail while trying to create the contact record. However, flow will not end. You can display a meaningful message using the FaultMessage global variable. In this case, only the action that caused the failure will not be saved to the database. Therefore, flow will create an account record but not a contact under it. Read this post to learn how to handle errors in flow.

Screen flow with error handling

Here is the result. The user will see the reason of failure, or anything that you want to display. However, flow will not roll back the transaction so it will create an account record without a contact.

Result if you handle the errors without roll back.

3- In the previous option, the user saw the correct error message, but the system created a missing data. In most cases, you will want to display the error message but not create missing data. In order to do so, you have to use the Roll Back Records in the fault path.

Roll Back Records element in faulth path

Here is the result. The user will see the fault message but the records will not be saved to the database.

Result of the flow with roll back records element.

Considerations

This element rolls back the current transaction, but the operations that were performed in the previous transactions won't be rolled back.

For example, this flow gets an input from the user and creates an account record. Then, there is another screen to get contact details and it creates a contact record. As mentioned before, screen element ends the transaction, so there are two different transactions in this flow. If contact creation fails and you use the Roll Back Records element, it cannot roll back the account creation since it was created in the previous transaction.

Flow with Roll Back Records element and two transactions

This element is available in any part of the flow. Although it is very useful in the fault path, you can use the Roll Back Records element wherever you want.

Be the first to comment

Leave a Reply

Your email address will not be published.


*