# How do Validation Rules enforce data quality standards and display custom error messages to users?

**Topic:** Admin · **Difficulty:** Medium · **Tags:** Admin, Validation Rules, Data Quality

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-validation-rules-enforce-data-quality-standards-and-display-custom-error-messages-to-users

## Answer

**Basics:** Required fields and picklists cannot express conditional business rules such as "Close Date cannot be in the past" or "Discount over 20% needs a reason". Validation rules express those. They run on create and update (not on delete) whether the record is saved by UI, API, Flow, Apex or data import.

**How it works:**
1. Setup > Object Manager > object > Validation Rules > New.
2. Write the Error Condition Formula (TRUE means error).
3. Provide an Error Message and choose Error Location: Top of Page or a specific field.
4. Activate. Use Check Syntax first.
5. In the order of execution, custom validation runs after system validation and before-save flows/before triggers have run, and again fires on final values; the rule sees the values after before triggers and before-save flows have updated them.

**Advanced / deep dive:** Useful functions: `ISBLANK`, `ISCHANGED`, `ISNEW`, `PRIORVALUE`, `REGEX`, `AND/OR/NOT`, `CASE`, cross-object fields through lookups, `$User`, `$Profile`, `$Permission` and `$Setup` (custom metadata/custom settings) to implement bypass switches. Use `ISBLANK` rather than `ISNULL` for text and numbers (`ISNULL` is only reliable for dates in some contexts). A bypass via `$Permission.Bypass_Validation` is better than profile names, which are brittle. Validation rules can block integrations and data loads, so scope them with `$User` checks or custom-permission bypass. They do not run on records deleted, nor on rollup changes that do not modify the child directly; a rule may also fire when a Flow or trigger updates the record for unrelated reasons, which can surprise admins. For complex logic that is hard to read, use Apex or a Flow with clear errors; for cross-record checks, validation rules only see parent fields through lookups, not child collections. Formula size limits apply (character and compiled-size limits); keep rules short.

**Code / config example:**
```
// Error when stage is Closed Won and Amount is blank, unless a bypass permission is granted
AND(
  NOT($Permission.Bypass_Validation),
  ISPICKVAL(StageName, "Closed Won"),
  OR(ISBLANK(Amount), Amount <= 0)
)
Error message: "Amount is required to close an opportunity as Won."
Error location: Amount
```

**Hidden gotchas & must-remember points:**
- The formula returns TRUE for invalid data; people write it backwards.
- Picklists need `ISPICKVAL`, not `=`.
- `ISCHANGED` is invalid on create; combine with `NOT(ISNEW())` where needed.
- Active rules also block API loads and flow updates.
- Error location on a field the user cannot see is shown at the top of the page instead; verify page layout behavior.
- Validation runs on Lead conversion only for the converted target records according to org settings; test conversions.

**Best practice:** Write specific error messages, include a permission-based bypass for data migration, and document every rule in the object's description.

---
Source: ForceChallenges · last updated 2026-11-02
