Recommended
Fixing Data in a before-save Flow instead of blocking users with Validation Rules (opens in a new tab)
You will learn how a before-save Record-Triggered Flow can correct a field value before a Validation Rule evaluates it, so users are not stopped by errors.
Why read this
You will also see when a Validation Rule should stay and how to make it less disruptive.
Validation Rule
- Checks the value as entered
- Blocks the save with an error
- User must fix the value by hand
Before-save flow
- Corrects the value before the check
- Record saves with no error
- Works for UI, API and Data Loader
The author opens with a sales manager whose reps kept personal spreadsheets because repeated validation errors made Salesforce slow to use. He argues that some Validation Rules can be replaced by logic that corrects the data instead of rejecting it.
A Record-Triggered Flow set to Fast Field Updates runs before the record is saved, ahead of Apex before triggers and Validation Rules. A corrected value is therefore what the Validation Rule checks, so the user sees no error. The same applies to every source that saves the record, including the UI, the API and Data Loader.
As an example, a rule requiring the Website field on Account to start with http or https can be replaced by a flow. The flow uses a formula resource that prefixes https:// when needed, then an Assignment element sets the field back on $Record. No Update Records element is needed, and the author advises testing in a sandbox and deactivating the rule while keeping its formula in the description. Other candidates include forcing uppercase codes, stripping dashes from tax IDs and deriving a region from a country.
Where the correct value cannot be inferred, as with a loss reason on a Closed Lost opportunity, the rule should stay but be made less painful. The author suggests showing the field only when relevant with Dynamic Forms, marking it required on the page, placing the error on the field, and scoping the formula with ISCHANGED(StageName).
Read the original at dev.to (opens in a new tab)