# How can validation rules enforce data hygiene on opportunity close dates and pipeline milestones?

**Topic:** Sales Cloud · **Difficulty:** Medium · **Tags:** Sales Cloud, Validation Rules, Data Hygiene

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-validation-rules-enforce-data-hygiene-on-opportunity-close-dates-and-pipeline-milestones

## Answer

**Basics:** A validation rule evaluates a formula on save. If it returns TRUE, the save fails and shows an error message. They enforce hygiene such as forecast accuracy and required milestone fields.

**How it works:**
1. Setup > Object Manager > Opportunity > Validation Rules > New.
2. Write a formula that is TRUE when the data is bad, and set the error message and location.
3. Rules run after before-triggers and before after-triggers, on UI, API, Flow and imports.
4. Use ISNEW(), ISCHANGED() and PRIORVALUE() to limit when the rule fires.
5. Use custom permissions ($Permission.Name) to allow managers to bypass.

**Advanced / deep dive:** Fire rules only when the relevant field changes, otherwise users cannot edit unrelated fields on old opportunities with past close dates. Closed deals often legitimately have past close dates, so scope to open opportunities. Stage-based rules (for example require Next Step or a contract field at Negotiation) use CASE or TEXT(StageName). Integrations and data loads also hit validation, so provide a bypass, such as a custom permission or hierarchy custom setting, and audit its use. Too many validations make forms frustrating; balance with Path guidance and Flow screens. Because rules also run on lead conversion and Apex DML, tests must create valid data.

**Code / config example:**
```
/* Block past close dates on open opps (unless bypass permission) */
AND(
  NOT($Permission.Bypass_Close_Date_Rule),
  NOT(IsClosed),
  OR(ISNEW(), ISCHANGED(CloseDate)),
  CloseDate < TODAY()
)
Error: Close Date cannot be in the past for an open opportunity.

/* Block close dates more than 365 days out */
AND(NOT($Permission.Bypass_Close_Date_Rule),
    OR(ISNEW(), ISCHANGED(CloseDate)),
    CloseDate > TODAY() + 365)
```

**Hidden gotchas & must-remember points:**
- The formula returns TRUE when the data is invalid.
- Without ISCHANGED or ISNEW the rule blocks all edits to old records.
- Rules run for API, Flow and Apex, not just the UI.
- Custom permissions give a clean bypass for admins and integrations.
- Validation runs after before-triggers, so trigger changes are validated.
- Closed opportunities need scoping to avoid blocking legitimate history.

**Best practice:** Scope rules narrowly with ISCHANGED, add a documented bypass, and write clear error messages.

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