function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nandhini S 3Nandhini S 3 

How to throw error using flow?

Hi All,

I'm writing a trigger to throw an error on record insert/update based on certain conditions. Is it possible to do the same using flows?
HarshHarsh (Salesforce Developers) 
Hi Nandhini,
  • Salesforce introduced a new element called Custom Error in the Winter '24 release.
  • This element lets you display error messages in record-triggered flows.
  • There was already a workaround (https://salesforcetime.com/2021/09/25/complex-validation-rules-using-record-triggered-flows/) to achieve this using a checkbox field that fires a validation rule. However, you had to create a checkbox field and a validation rule for each error message that you wanted to display. Moreover, this workaround worked only before saving record-triggered flows (excluding before delete).
  • Using fault paths for error handling is very important. Using fault connectors, users can understand what went wrong and take actions about it. If it is a screen flow, you can connect the fault connectors to a screen and display an error message. To display the actual error message, you can simply use {!$Flow.FaultMessage}. On the other hand, if you handle errors in record-triggered flows, there is no way to display an error message (except for the workaround for before-save record-triggered flows).
  • The new Custom Error element solves this issue. You can use this element in fault paths to display error messages in record-triggered flows.
For more details, you can refer to the following link 
https://salesforcetime.com/2023/08/04/display-custom-error-messages-in-record-triggered-flows/

You also can refer to  Step by Step Guide to “Custom Error” in Record-Triggered Flow (https://www.asagarwal.com/step-by-step-guide-to-custom-error-in-record-triggered-flow/).

Please mark it as Best Answer if the above information was helpful.

Thanks.
Ritta ClarkRitta Clark
Yes, it is possible to throw an error using flows. You can use the Custom Error element to throw an error with a custom message.
To do this, follow these steps:
Create a new flow.
Add the Custom Error element to the flow.
In the Error Message field, enter the custom error message.
In the Error Code field, enter the error code.
Save the flow.
When the flow is triggered, if the conditions are met, the Custom Error element will throw an error with the custom message and error code.
Here is an example of how you can use the Custom Error element to throw an error on record insert/update based on certain conditions:
Trigger: Record Insert/Update
Action: Custom Error
Error Message: "This record cannot be inserted/updated because it does not meet the required criteria."
Error Code: 12345
This flow will throw an error with the message "This record cannot be inserted/updated because it does not meet the required criteria." and the error code 12345 when a record is inserted or updated that does not meet the required criteria.