Learner's Byte logo goes here
+
Oscar IT Solutions logo goes here

"Galat data andar aaya, toh galat report bahar jayegi."

"If wrong data comes in, wrong reports go out." Validation rules are the gatekeeper at the door.

  • What counts as bad data?
  • How do I write the formula?
  • What should the error say?
  • When does it run?
  • When is it the wrong tool?

Step 3: Validation Rules

Step 2 gave each fact one home. Now we make sure only good data moves in. A validation rule filters the data before Salesforce stores it, and tells the user exactly how to fix it.

You need from Step 2
Your three models, built in Org A and Org B
You will build
About 25 validation rules and one duplicate rule, across three projects
Suggested time
3 live sessions of 90 minutes, plus about 5 hours of practice
AI skills used
Coding and Automation, Reasoning and Problem Solving, Classification

You are hereSteps 1-4 Requirements and configurationSteps 5-9 DevelopmentSteps 10-11 Test and deploySteps 12-15 Rollout

Hook: spot the bad data

These six records were saved into the academy's system before it had any rules. Click each record you think is wrong.

Not one of these broke the system. Each one was saved happily. Then the reminder went to a wrong number, the director's report showed a batch at 125% capacity, and an admission was counted with no payment behind it.

Byte This: Cleaning bad data later costs far more than stopping it at the door.

By the end of this workbook, you will be able to

  • Turn a business rule into a "bad condition", then into a formula.
  • Write error messages that tell users exactly how to fix the problem.
  • Use the core functions: ISBLANK, ISPICKVAL, ISNEW, ISCHANGED, PRIORVALUE, REGEX, TODAY and cross-object references.
  • Know when validation rules run, and when they are the wrong tool.
  • Build a safe bypass for data migration with a custom permission.
  • Test every rule with passing, failing and boundary cases.

Concept: how a validation rule thinks

1. What a validation rule is

A validation rule belongs to one sObject. When anyone tries to save a record, Salesforce checks the rule's formula:

  • If the formula is TRUE, the data is bad. Salesforce blocks the save and shows your error message. Nothing is stored.
  • If the formula is FALSE, the data passes this rule.
Rule NameA clear name, such as BATCH_Capacity_Range
DescriptionThe requirement it enforces, such as TMS-03
Error Condition FormulaDescribes the bad data. TRUE blocks the save.
Error Message and LocationWhat the user reads, shown at the top of the page or next to one field
The one idea that trips everyone: You don't write what good data looks like. You write what bad data looks like. The formula answers the question "Should I stop this save?"

2. From business rule to formula, in four moves

  1. Say the rule in plain words: "A batch holds 1 to 30 students."
  2. Flip it to the bad case: "Bad when Capacity is below 1 or above 30."
  3. Translate to a formula, using field API names.
  4. Write the fix as the message: "Capacity must be between 1 and 30 students."
Capacity__c < 1 || Capacity__c > 30

3. The building blocks

Two gotchas: A Percent field is a decimal inside a formula, so 25% is written as 0.25. And a picklist can't be compared with =. Use ISPICKVAL(Status__c, "Paid"), or ISBLANK(TEXT(Status__c)) to check for blank.

4. Eight patterns you will use again and again

Almost every validation rule in real projects is one of these.

5. Error messages that help

Weak messageWhy it failsStrong message
Invalid data.Which field? What is valid?Enter a 10-digit mobile number without +91 or spaces, like 9848011111.
Error in date.The user can't tell how to fix it.End Date must be on or after Start Date.
Rule TMS-03 violated!Internal codes mean nothing to staff.This batch is full. Choose another batch or ask the coordinator to add seats.

Location: Put the message next to the field when one field is wrong. Use the top of the page when the problem involves several fields.

6. When validation rules run

They run on every save, wherever the save comes from: the record page, a list view inline edit, Data Loader, the API, a Flow or Apex. That is what makes them stronger than a "Required" tick on a page layout, which only works on screen.

  • Order: Before-save flows and before triggers run first. Validation rules then check the final values, and only then is the record stored.
  • Existing data isn't rechecked. A new rule only applies when a record is next created or edited. Old bad records stay until someone edits them.
  • Lead conversion: Rules on Account, Contact and Opportunity run during conversion only if Lead Settings > Require Validation for Converted Leads is on.
  • What a rule can see: fields on this record, its parents through relationships (like Batch__r.Capacity__c), the user and the date. It can't see other child or sibling records.

7. Is a validation rule the right tool?

Use the simplest tool that does the job. Validation rules are powerful, but each one adds something to maintain.

8. The safe bypass

In Step 13 you will load thousands of old records, and some will break today's rules. Never switch rules off in production, and never hard-code a username or profile name. Do this instead:

  1. Create a Custom Permission called Bypass_Validation, in Setup > Custom Permissions.
  2. Add it to a Permission Set called Data Migration Bypass.
  3. End each rule that may need bypassing with && NOT($Permission.Bypass_Validation).
  4. Assign the permission set only to the migration user, only during the load. Remove it afterwards.

9. Test every rule

TestExample for capacity 1 to 30Expected
A clearly good valueCapacity 20Saves
A clearly bad valueCapacity 45Blocked, with the right message
Each boundary1 and 30, then 0 and 311 and 30 save, 0 and 31 are blocked
BlankCapacity left emptyDecide on purpose, and test it
Editing an old recordChange only the Batch nameShouldn't be blocked by an unrelated rule

Demo: Project 1, Training Management System

Watch the four moves on Akshara Skills Academy, using the model you built in Step 2.

Try it: save a Batch

These are the Batch rules below, running in your browser. Change values and press Save. Try to make each rule fire.

Try it: save a Student

All the Project 1 rules

Notice: FEE_Refund_Window uses ISCHANGED. Without it, every later edit to that instalment would be blocked once the 7 days pass, even fixing a typo.

What validation rules can't do here

RequirementWhy a validation rule isn't enoughWhere it is handled
TMS-03, under heavy loadENR_Batch_Full reads Seats Filled as it was before the save. If Data Loader inserts many enrolments into one batch together, they all see the old count.Step 9 makes it bulletproof
TMS-09 Certificate eligibilityNeeds attendance percentage across many Attendance records, which a rule can't seeStep 9
TMS-12 Discount above 10% needs approvalA rule can only block or allow, it can't route a request to RaviStep 14, with an approval process

Build it in Org A

  1. Open Setup > Object Manager > Batch > Validation Rules > New.
  2. Enter the Rule Name and a Description with the requirement ID, such as "TMS-03: batch holds 1 to 30 students".
  3. In Error Condition Formula, use Insert Field to pick fields, so API names are exact. Click Check Syntax.
  4. Write the Error Message and choose its Error Location. Save.
  5. Repeat for every rule in the table above.
  6. Create the Bypass_Validation custom permission and permission set from Concept 8. Add the bypass to the rules marked "Yes".
  7. Run the tests from Concept 9 for each rule. Record what happened in your Rules Pack.

Practice: Project 2, Hospital Management System

Now you write the formulas, with support. Then you add a second filter Salesforce offers: a duplicate rule.

Patient data rule: Use only invented patients in your practice org, never real details.

Exercise 1: Which tool filters this best?

Exercise 2: Pick the correct formula

Each requirement has one correct formula. The others contain mistakes beginners really make.

Exercise 3: Write the Arogya Care rules

Your turn

For each requirement, do the four moves and add the rule in Org A. Record each one in the Project 2 tab of your Rules Pack.

  1. A new appointment can't be booked in the past.
  2. A Completed appointment must have a Diagnosis.
  3. An Emergency visit must have a Triage Level. Step 4 turns Emergency into a record type.
  4. Patient mobile is a valid Indian mobile number, if given.
  5. Date of Birth can't be in the future.
  6. A patient under 18 must have a Guardian.
  7. A Released lab order must have a Result Value.
  8. A result can't be changed after it is Released, except by someone with the Amend_Lab_Result permission.
  9. A waiver can't be more than the bill Total, and any waiver needs a reason.
  10. A cancelled appointment can't be moved back to Booked.
Open the Project 2 model answer after you finish

Exercise 4: Stop duplicate patients (HMS-01)

A validation rule can't compare one record with another. Salesforce has a separate filter for that: matching rules and duplicate rules.

  1. Go to Setup > Matching Rules > New Rule. Choose Patient. Match on Mobile (Exact) AND Date of Birth (Exact). Save and Activate.
  2. Go to Setup > Duplicate Rules > New Rule and choose Patient.
  3. Set Action On Create to Block, with the message "This patient already exists. Search by mobile number first." Set Action On Edit to Allow, with an Alert.
  4. Select your matching rule, Save and Activate.
  5. Test it: create a patient, then try a second with the same mobile and date of birth.

What remains for later steps

  • HMS-04, 30 outpatients per doctor per day: needs a count of sibling Appointments on the same day. A validation rule can't see those, so Step 9 handles it.
  • HMS-13, waiver above ₹5,000 needs approval: Step 14.

Project: Project 3, CRM for Training Management

Do this one yourself, in Org B. Standard sObjects already bring some rules of their own, so check before you add.

Rules Salesforce already gives you

  • An Opportunity needs a Name, Stage and Close Date.
  • A Lead needs a Last Name and, for a Business Account, a Company.
  • Standard picklists like Stage only accept their listed values.

Standard field API names you will need

Your turn: build the Admissions rules
  1. An Opportunity can't move to Admitted without a Payment Reference (CRM-06).
  2. A Lost Opportunity needs a Lost Reason.
  3. An open Opportunity's Close Date can't be set in the past.
  4. A Lead needs at least a Mobile or an Email.
  5. A Lead's mobile must be a valid Indian mobile number, if given.
  6. A Campaign's End Date can't be before its Start Date, and Actual Cost can't be negative.
  7. A Person Account's Birthdate can't be in the future.
  8. A Case can't be Closed without a Resolution.
  9. Decide whether to turn on Require Validation for Converted Leads, and write down why.
Open the Project 3 model answer after you finish

On converted leads: Turn it on. Otherwise a lead converted straight to an Admitted opportunity could skip CRM-06 entirely.

Build your Rules Pack

Your work saves in this browser on this device. Use "Download my work" regularly so that you keep a copy.

Build with AI

Write your first rules by hand. AI is excellent at formulas, but it can't know your client's exceptions, and a wrong rule blocks real staff from doing their work.

Three rules: Give AI your exact field API names, or it will invent them. Test every AI formula with Check Syntax and with the five tests from Concept 9. Never deploy a rule you can't explain in one sentence.

Reflect and exit check

Quick test

Exit check: are you ready for Step 4?

What Step 4 needs from you: Step 3 decides what data is allowed. Step 4 decides who sees and edits each field, and which form they use. Some rules here, like Corporate and Emergency, will move onto record types.

Interview preparation

Practise saying these answers aloud. Then take them into your text and voice interview practice with our AI agents, and work on the feedback you get.

For your resume and portfolio

Describe this honestly as training project work:

Training project: Designed and tested validation rules and a duplicate rule for a simulated hospital system on Salesforce, each traced to a business requirement, with a custom-permission bypass for data migration.
  • Keep your Rules Pack, with the test results, as portfolio evidence.
  • Be ready to write one formula live, such as "Admitted needs a Payment Reference".

Trainer notes

Open the suggested session plan and coaching notes
SessionCoverStudents leave with
1 (90 min)Hook. Concepts 1 to 5. Students break the two simulators on purpose. Build two Batch rules live.Project 1 rules as homework
2 (90 min)Concepts 6 to 9. Exercises 1 and 2. Build the bypass and the duplicate rule live.Project 2 rules as homework
3 (90 min)Review tests together. Standard objects in Org B. Start Project 3.Project 3, reflection and exit check

Common mistakes to catch

  • Writing the good condition. Ask: "Should this formula stop the save?"
  • Using = on a picklist. Show the syntax error, then ISPICKVAL.
  • Writing 25 instead of 0.25 for a Percent field.
  • Rules that block every later edit of an old record. Show ISNEW and ISCHANGED.
  • Hard-coding a profile name or user to bypass a rule. Show the custom permission.
  • Trying to count sibling records in a rule. Point to Step 9.

REGEX in Salesforce

Salesforce REGEX must match the whole value, so [6-9][0-9]{9} needs no anchors. Students coming from other languages often add them; both work, but explain why they're not needed.

Check before each batch

Confirm the setup paths and the standard API names in a fresh Developer Edition org, since Salesforce updates its menus every release.

Sign-off standard

A rule passes when it traces to a requirement, its message tells the user how to fix the problem, it has passing, failing and boundary tests recorded, and it doesn't block unrelated edits.