Custom Validations Using Record-Triggered Flows

Sneha Likhar
July 18, 2024

In today's rapidly evolving Salesforce development arena, maintaining data integrity and enforcing business logic stand as top priorities. Thanks to the advancement of automation tools such as Record-Triggered Flows, developers now possess a formidable tool for crafting custom validations that precisely match their organization's specific needs. 

No longer constrained by the limitations of cumbersome validation rules and intricate Apex triggers, Record-Triggered Flows offer a streamlined and intuitive approach to executing intricate business processes, including validation logic, seamlessly integrated within the Salesforce platform. 

In the Winter '24 release, Salesforce introduced an innovative feature known as Custom Error. This functionality enables users to display error messages directly within record-triggered flows. Before this release, users had to resort to a workaround involving a checkbox field that triggered a validation rule. However, this approach required creating a separate checkbox field and validation rule for each error message required. Moreover, this workaround was limited to before-save record-triggered flows, with exceptions for before-delete scenarios. 

Objectives: 

After reading this blog, you’ll be able to: 

• Understanding of validation rule and its limitations 

• User record-triggered before-save flow to create an error message. 

• Show the error message at the Top of the Page 

Business Use case: 

Steve Smith, a Salesforce Administrator at Sierra Cloud, has been tasked with a specific requirement. He must restrict users from creating a new recurring task if Type is not filled within the task. The following text should be utilized for the error message: • “If you are creating a Recurring Series of Tasks then You must fill the Type”. 

Validation Rule: 

A Validation Rule empowers a System Administrator to establish personalized logic and error messages to uphold data integrity. This rule may comprise a formula or expression that assesses data across one or multiple fields, yielding either a True or False outcome. For instance, if the rule returns a True value, signalling inaccurate data input, an error message is triggered. It's crucial to note that a Validation Rule activates solely during record creation or editing processes.

In the given business scenario, a validation rule would be ineffective as the Is Recurrence field cannot be utilized due to Salesforce's limitations. 

Understanding Record-Triggered Flows: 

Record-Triggered Flows are triggered when specified changes occur to a record in Salesforce. Whether it's upon creation, editing, or deletion, these flows spring into action, allowing developers to define a series of actions to be performed based on predetermined criteria. 

The Power of Custom Validations: 

One of the most compelling use cases of Record-Triggered Flows is custom validations. While Salesforce offers standard validation rules, they may not always align perfectly with an organization's specific needs. Custom validations bridge this gap by allowing developers to enforce business rules tailored to their domain.

Benefits of Custom Validations Using Flows: 

1. Granular Control: Record-Triggered Flows empower developers to implement validations at a granular level, ensuring that data integrity is maintained with precision. 

2. Complex Logic: Unlike traditional validation rules, flows offer unparalleled flexibility in implementing complex validation logic. From cross-object field comparisons to dynamic validation criteria, the possibilities are endless. 

3. Enhanced User Experience: By providing custom error messages and actionable insights, developers can enhance the user experience, guiding users towards correct data entry practices.

What is Before Save Flow? 

1. The Before-Save Flow acts as a trigger executed before database operations like insertions, updates, or deletions. It's utilized to verify or modify values prior to data being committed to the database. Unlike traditional processes, a Before-Save Flow is more efficient as records are not saved to the database again, omitting additional rounds of Assignment rules, Auto-response rules, Workflow rules, and other time consuming customizations.

2. Ideal use cases for a Before-Save Flow include updating fields on new or modified records. 

My Approach: 

1. With the Winter '24 release, you now have the capability to craft intricate validation rules that were previously unattainable via standard validation rules alone. This achievement is made possible by leveraging the before-save flow in conjunction with the newly introduced custom error component. 

2. Before delving into the solution, take a moment to review a high-level diagram illustrating the process. Familiarizing yourself with this Flow diagram will provide a clear understanding of the upcoming solution.

Let’s begin building this automation process. Guided Practice (We-do): 

To address Steve's business requirement utilizing Record-Triggered Before-Save Flow and the recently introduced Custom Error element, we will follow these below steps: 

1. Define flow properties for the record-triggered flow. 

2. Implement another decision element to confirm the presence of Is Recurrence and Type.

3. Introduce a Custom Error element to display customized error messages as needed. 

Step 1: Define Flow Properties: 

1. Click Setup

2. In the Quick Find box, type Flows

3. Select Flows, then click on the New Flow

4. Select the Record-Triggered Flow option, click on Create. 

a. Object: Task 

b. Trigger the Flow When: A record is created. 

c. Set Entry Conditions: None 

d. Optimize the Flow For: Fast Field Updates 

5. Click the X at the top to save your changes.

Step 2: Using Decision Element to Check if the task is IsRecurrence and Type is Null: 

1. On Flow Designer, click on the +icon and select the Decision element.

2. Enter a name in the Label field; the API Name will auto-populate. 

3. Under Outcome Details, enter the Label the API Name will auto-populate.

4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)

i) Row 1: 

a. Resource: {!$Record.IsRecurrence} 

b. Operator: Equals 

c. Value: {!$GlobalConstant.True} 

ii) Row 2: 

a. Resource: {!$Record.Type} 

b. Operator: Is Null 

c. Value: {!$GlobalConstant.True} 

5. When to Execute Outcome: If the condition requirements are met.

6. Click Done.

Step 3: Adding a Custom Error Element to Show Error Messages: 

1. Under the Yes node, select Custom Error element. 

2. Enter a name in the Label field; the API Name will auto-populate. 

3. Set Error Message 1 Details 

a. Where to Show the Error Message: In a window on a record page 

b. Error Message: If you are creating a Recurring Series of Tasks then You must fill Type. 

4. Click the X at the top to save your changes.

In the end, Steve’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

1. Click Save

2. Enter Flow Label the API Name will auto-populate. 

3. Click Show Advanced

4. API Version for Running the Flow: 60 

5. Interview Label: Custom validation Rule Using Flow {!$Flow.CurrentDateTime}

6. Click Save

Almost there! Once everything looks good, click the Activate button. 

Things to Remember: 

• The error message field has a character limit of 255. 

• You can only include one error message per page. 

• Only one error message can be added per field. 

• Linking with your error message isn't possible. 

• The escape parameter determines if HTML markup in the custom error message should be escaped (true) or not (false). However, in Lightning Experience and the Salesforce mobile app, HTML is always escaped. The escape parameter is only relevant in Salesforce Classic. 

• Read this article for more information. 

Proof of Concept: 

Starting now, whenever a user endeavors to create a new recurring task without filling the Type, the record-triggered before-save flow we've established will activate and showcase an error message. 

References:

Contact us
Contact us today to schedule your free Sierra PulseCheck and discover how our Altitude Services can maximise your platform's potential for growth and innovation.
Address:
Australia Offices
Sydney & Melbourne
Contact:
Address:
Jaipur, India Office
4th Floor, 413-415 , Signature Tower, Lal Kothi Tonk Road, Jaipur 302015
Contact:
© 2022 Sierra Cloud. All right reserved.