Salesforce Flow Builder allows you to build complex business automations using clicks instead of code. It is the most powerful tool that Salesforce admins have and it gives you similar powers that Salesforce developers have. However, like all the automation tools that exist, there is a chance that your flow will fail and won't perform the actions it supposed to perform.
What Happens When a Flow Fails?
When a flow interview fails, the running user gets an error message which is not clear at all.
The running user cannot continue or go to the previous step after this error. Salesforce sends an email to the user that last modified the flow or users/emails that are defined in the Apex Exception Email with details about everything that was executed and what failed. The details include the data that's involved in the process or flow, including user-entered data. This can be defined in Process Automation Settings from the setup.
How to Handle Flow Errors?
Fault Connector can be used to handle flow errors. Using Fault Connectors, users can understand what went wrong and take actions about it.
It is possible to use a Fault Connector from any of the data (create, get, update, delete) or action elements. To create a Fault Connector, you have to create a second connection from your element after creating your first connection.
If you are using a Screen Flow, you can connect the Fault Connectors to a screen and display an error message. Since it is a screen that you build, you can use rich text and images to make the error message look more friendly. To display the actual error message, you can simply use {!$Flow.FaultMessage}. For example, if the flow is failed because of a validation rule, then the validation rule's error message will be displayed. As a best practice, use assignments to store the part that the flow failed and display it on the screen as well. So that the running user will have more information about what has failed. You can also add other actions like creating a record (for example a case record) or sending an email about the error.
Display a message on a screen can be done only in Screen Flows. If you are using other flow types like autolaunched flows or record-triggered flows, an error message will be displayed on the screen where you are triggering the flow.
When you add a Fault Connector to a flow and if the flow is getting failed, the flow action will be failed but the first action that triggered the flow will be success, since the exception is handled. This is same as an Apex class behavior when you add try - catch block. Since the exception is handled, the running user will not see an error message.
For example, let's say that you have a flow that runs when you create a contact and the flow updates a field on the account. If the field update on the account fails but you used a Fault Connector to handle this error, then the field update will not be performed, but the contact will be saved. If you don't use a Fault Connector to handle this error, also the contact will not be saved. To fail the complete transaction after handling the error, you can use an invocable Apex method, which can throw a flow exception.
To bypass errors for a given element in your flow, draw the fault connector to the same element as the normal connector.
Sending Better Error Emails
The standard error email that Salesforce sends is a little hard to read. First of all it is too long because it contains all the steps of the flow (if you have a loop in your flow, expect a very long email). Secondly, it doesn't contain any custom messages which will make you understand what went wrong. You just need to follow the steps and try to understand what went wrong.
Good news is that you can create your own error email. Use assignments in your flow to store some custom information like the location that the flow failed. Then create a text template resource and build the body of your email. Don't forget to include {!$Flow.FaultMessage}, values of some of the important variables in the flow, running user's name and date/time of the failure.
Then use the send email action to send this text body to the admins or any user/email address that you want.
Using Subflows For Exception Handling
Fault Connector is a really great solution to handle the errors and you should use them in all of your flows. However, it takes time to build it, right? Should you build the same thing over and over? The answer is no!
You can create a flow that will do all the actions that you want to perform when a flow fails. Then you can call this flow (as a subflow) from any of your flows. This way, you will build the exception handling only once and use it from anywhere. Don't forget to pass the error message and a few more details as parameters to your subflow!
Leave a Reply