In this session, we will talk about how to handle exceptions/errors in Salesforce flow using fault connectors and the Platform events framework. Let learn about Exception handling in flow.
Handling & Logging
Let understand the different between Exception handling and Logging:
- Exception Unexpected event occurred during your transaction execution
- Exception Handling Deal with unexpected Exception
- Exception Logging Store exception details for later analysis and troubleshooting
Let see how to handle exceptions in a Salesforce flow using fault connector, and what will be the transaction behavior after adding a fault connector to the flow.
Exception handling in flow Scenario 1
Suppose we have one flow which updating Account with contacts country. like below which invoked from process builder.
![Exception handling in flow](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/08/image-4.png?fit=1024%2C310)
Create Exception Scenario
Let try to add some exception with the help of validation rule. Let us add a validation rule under Account which makes Account Number mandatory on Account update.
![Create Exception Scenario](https://www.apexhours.com/wp-content/uploads/2020/07/image-6.png)
Now if account number is missing on account record then our flow will fail. Now let see how we can handle this exception.
Handle & Log Exception – Exception Object
As a best practice, Lets try to log all exceptions in an Exception custom Object.
![Handle & Log Exception – Exception Object](https://i0.wp.com/www.apexhours.com/wp-content/uploads/2020/08/image-5.png?fit=1024%2C565)
How to handle Exception in a flow?
To handle Exception, we need to add a fault connector to the record update operation as shown below.
What is Fault Connector?
Identifies which element to execute when the previous element results in an error
![What is Fault Connector?](https://www.apexhours.com/wp-content/uploads/2020/07/image-8.png)
Let see what will happened after adding flow connector ?
Handle & Log Exception – Transaction Behavior
![Handle & Log Exception – Transaction Behavior](https://i2.wp.com/www.apexhours.com/wp-content/uploads/2020/08/image-6.png?fit=1024%2C524)
- Contact Creation is success
- Account Country is not populated
- Exception Record Got Created
- Similar to try-catch in Apex
Handle & Log Exception – Throw Exception
What about if you want to fail the complete transaction ? But we have seen that we have a fault connector – transaction will be a success.
- No standard Approach in Flow
- Make use of Apex Throw exception
![Handle & Log Exception – Throw Exception](https://i0.wp.com/www.apexhours.com/wp-content/uploads/2020/07/image-9.png?fit=1024%2C235)
In Apex classes you have an option to throw exception after catching and logging error details. So let us see if we can add that behavior to a flow. We can use the invocable Apex method, which can throw our flow exception.
![Handle & Log Exception – Throw Exception](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/07/Throw-Exception-from-Flow.png?fit=1024%2C488)
This issue can be solved by logging exception using Platform events
Exception Logging – Platform events
![Exception Logging – Platform events](https://i0.wp.com/www.apexhours.com/wp-content/uploads/2020/07/Platform-Event-to-handle-Exception.png?fit=1024%2C533)
Platform events – Publish Behavior
![](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/07/Platform-events-%E2%80%93-Publish-Behavior.png?fit=1024%2C438)
Why we need PE & Exception Object?
PE does not provide:
- Reporting
- Querying
- Formula fields for additional processing
![](https://www.apexhours.com/wp-content/uploads/2020/07/image-10.png)
Scenario 2) Screen flow – Exception Logging
If the Account type is Partner Account – Display a screen to enter additional details by user and based on entered values, decide Account Partnership level as Diamond, Gold or Silver
![Screen flow – Exception Logging](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/07/image-11.png?fit=1024%2C326)
Screenflow – Exception Logginig using FlowExecutionErrorEvent
- FlowExecutionErrorEvent – Standard Platform event from Salesforce
- Salesforce publish event to this when a screen flow fails
- We can subscribe to this and log Exception
![](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/07/image-12.png?fit=1024%2C341)
Comparison between Custom Platform event and FlowExecutionErrorEvent approach for Exception Logging
![](https://i1.wp.com/www.apexhours.com/wp-content/uploads/2020/08/image-8.png?fit=1024%2C360)
Exception handling in flow Video
Agenda :
- Flows Use Cases
- Exception Handling & Logging
- Exception Object & Fault Connectors
- Transaction Behavior
- How Platform Events Helps
Thanks Meera Nair for sharing your knowledge with us.
GREAT….Very Helpful, thanks for sharing your knowledge.
One small suggestion – it would be great if you can embed the code snippets instead of screen-shots.
We will take care next time.
Hello Meera,
Thanks for the youtube video.
One question: For update record, we will have a collection variable to update. What if only 1 out of 10 records failed. I want to create exception record for only one record the failed one. Do you know how do we handle this in flows. Does the flow update/create record returns the same Database.SaveResult[] same as returned by Database.insert /update/upsert
Or Its AllORNone = true for flows?
Please throw some light on this if you can
Hi Raman – did you ever get an answer to your question? If 1 of many in a collection variable fails will the others succeed as is the case in Apex if using Database.insert and Database.SaveResult or is it All-or-none with Flow?
Did you get an answer to this? I was wondering the same thing.
Helpful video – in non-screen flows, is there a way to create a pop up notification under certain conditions?
How can we add fault connectors in before save flows(Fast Update)? I don’t see any fault connectors for before save flows. How can we do exception handling for before save flows if we don’t have an option to create records?