LinkedIn

Salesforce Record Triggered Flows - Create custom error messages

 



1. Introduction

As part of Winter 24 release, Salesforce released this much-awaited flow feature by which we can fire custom error messages to users as part of record-triggered flows. This feature is available as part of both before and after save record triggered flows.

We can add error messages to a specific field or at the page level. Let us see 2 use cases to demonstrate this feature.

2. Use case 1 - Before Save Flows

Scenario - When an Account is getting deactivated and if there are any active Contacts under that Account, deactivation should be failed with a custom error message.

Implementation steps:

1. Create an isActive checkbox in contact


2. Create a before record triggered flow in Account to check for active contacts when Account Active value becomes false:



Add entry criteria as Active field update:


 Now fetch active contacts under that Account:


Now add a decision element to check if the count >0


if a contact exists add custom error


we can see Custom Error available as a new element now.

in this add error message:


Here you can see we are getting 2 options.

1. Add error to record page

2. Add error to specific field

In this example - we are adding an error message to Account Active field.

The final flow looks like below:


Now if we have an account with Active contact and try to deactivate the contact, we will get an error message as shown below:


3. Scenario 2 - After Save flow

The above one was before save validation. Now let us see another scenario where a record update is getting failed and we need to show a custom error message to the user.

Scenario - Whenever a new opportunity is getting created, create and assign a task to the opportunity owner. In case the task creation is failed, show a custom error message to the user in the opportunity creation screen.

Let us add the below validation rule in Task:



The above validation rule makes sure that a type should be populated whenever a new task is getting created.

Now create the flow for task creation.

The flow will be fired whenever a new opportunity is created:


Create a task




The above creation will fail because of the validation rule we have on the task.

To handle errors in flow we will add a fault connector:



The task creation failed custom error looks like the below:


{!$Flow.FaultMessage} - will give the actual error details and we are adding a custom message "Task creation to owner failed" to that.

Now if we try to create a new opportunity, it will fail with the below error screen:




In the above message we can see our custom error message is displayed appended with error details.

4. Advantages

1. Earlier when we wanted to add complex validations to a record save operation, Apex trigger was the only option if it cannot be achieved using a validation rule. Now we can implement this using a flow also.

2. On unexpected exceptions from flow it was showing system error messages always. Now we can replace that with custom messages.

5. Demo



6. References

https://help.salesforce.com/s/articleView?id=release-notes.rn_automate_flow_builder_create_custom_error_messages_in_record_triggered_flows.htm&release=246&type=5

Comments

  1. Hi Meera,Thanks for sharing.Is the feature GA now .?? and one more question - is the update rollbacks automatically when error condition meets..??

    ReplyDelete

Post a Comment

Popular posts from this blog

Subscribing to Salesforce Platform Events using External Java Client - CometD

Salesforce Security - Restriction Rules and Scoping Rules

How to develop reusable Invocable Apex methods for Flows