Why validation in Screen Flows matters
Validation keeps your data accurate and prevents downstream errors. In Salesforce Screen Flows (Flow Builder), you can validate user input both client-side (immediate, inline) and server-side (after business logic checks). This post explains the available approaches with examples and best practices so you can surface meaningful validation errors to users.
1) Built-in Screen Component Validation (recommended for inline checks)
Each Screen component supports configuration-based validation. Use the “Component Validation” option to write a formula that evaluates to true when input is invalid, and supply a custom error message. This produces an inline error shown next to the field.
Example: validate a positive number for a variable {!Amount}
Formula to validate: {!Amount} < 0
Error message: Amount must be zero or greater.
Example for required email format using REGEX:
Formula: NOT(REGEX({!Email},"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"))
Error message: Enter a valid email address.
2) Flow-level (server-side) validation via Apex or Flow logic
For complex business rules that require database checks or logic that’s not supported by screen formulas (for example cross-record validations), perform the checks using:
- An Apex @InvocableMethod that returns success / error information
- A Flow Action (Apex action) that returns a wrapper pointing to an error message
- Flow Decision elements to branch on validation results and show a Screen with the error message
Pattern:
- Call Apex action or run Flow logic.
- If validation fails, route to a Screen element with a Display Text (or Rich Text) showing the returned error message.
- Allow user to go back to the input screen or cancel.
Simple invocable return wrapper (outline):
public class FlowValidationResult {
@InvocableVariable
public Boolean success;
@InvocableVariable
public String errorMessage;
}
// @InvocableMethod method would return List<FlowValidationResult> with success=false and errorMessage set when validation fails
3) Handling system errors (faults) in Flow
If a Flow element throws a system exception (Apex action, Record Create, Update), you can connect the element’s Fault connector to a Screen. That Screen can display a friendly message and guidance. While Flow exposes the underlying error details on the Fault path, the best practice is to display a friendly message and log the technical details for administrators.
4) Using custom Lightning components for advanced UX
If you need inline, field-level server validation (for example, show field-by-field server-side validation messages), build a custom LWC/Aura Screen Component that accepts Flow variables, performs validation (via @wire or Apex), and renders errors inline. Use the component’s API to return validated values and error states back to the flow.
Best practices
- Prefer screen component validation for simple checks — it’s immediate and user-friendly.
- For multi-record or DB-dependent validations, use Apex/Flow logic and route failures to a Screen that displays the message and guidance.
- Avoid showing raw system exceptions to end users. Use friendly messages and log details for support.
- When building custom components, make them idempotent and return structured validation results so Flow can act on them.
Summary
Yes — you can show validation errors in Screen Flow. Use built-in component validations for inline checks, Flow/Apex for server-side rules, Fault connectors to capture system errors, and custom LWC components when you need advanced, inline server-validated UX.
Leave a Reply