Creating Reports and Dashboards in Salesforce

How to Create Validation Rules in Salesforce

Validation Rules in Salesforce are used to ensure data quality and business logic by preventing users from saving incorrect or incomplete data.

They work by checking the data entered in a record and showing an error message if it doesn’t meet your criteria.


πŸ” What Is a Validation Rule?

A Validation Rule is a combination of:

  • A logical formula or expression (returns TRUE or FALSE).

  • An error message (displayed if the formula returns TRUE).

If the formula is TRUE, Salesforce blocks the record from being saved.


πŸ› ️ When to Use Validation Rules?

Use Validation Rules when you want to:

  • Ensure required fields are filled under certain conditions.

  • Prevent invalid data entries (e.g., closing a case without a reason).

  • Limit values based on user roles or profiles.

  • Match business processes (e.g., discount can’t exceed 30%).


✅ Real-Life Examples

  • A phone number must be 10 digits.

  • Close Date can’t be in the past.

  • Opportunity amount must be greater than zero.

  • Email must end with “@company.com”.


πŸ”§ How to Create a Validation Rule in Salesforce (Step-by-Step)

Step 1: Go to Object Manager

  1. Log in to Salesforce (Setup).

  2. Click on Object Manager.

  3. Choose the object (e.g., Account, Contact, Opportunity).


Step 2: Navigate to Validation Rules

  1. Inside the object, click on Validation Rules in the left menu.

  2. Click the New button.


Step 3: Define the Rule

  1. Rule Name: Give it a clear name (e.g., Phone_Must_Be_10_Digits).

  2. Description: Explain the rule’s purpose.

  3. Error Condition Formula: Write the formula that must be validated.

  4. Error Message: Write a user-friendly message.

  5. Error Location: Choose whether to show the error at the top or near a specific field.


πŸ§ͺ Example 1: Phone Number Should Be 10 Digits

Formula:

LEN(Phone) <> 10

Error Message:

"Phone number must be exactly 10 digits."


πŸ§ͺ Example 2: Close Date Cannot Be in the Past

Formula:

CloseDate < TODAY()

Error Message:

"Close Date cannot be in the past."


πŸ§ͺ Example 3: Only Managers Can Approve Over 30% Discount

Formula:

AND(Discount__c > 0.3, $Profile.Name <> "Sales Manager")

Error Message:

"Only Sales Managers can give a discount of more than 30%."


πŸ§ͺ Example 4: Mandatory Field When Stage is ‘Closed Won’

Formula:

AND( ISPICKVAL(StageName, "Closed Won"), ISBLANK(Reason_Won__c) )

Error Message:

"Please enter a reason for the win."


πŸ§ͺ Example 5: Email Must Be Company Domain

Formula:

NOT(CONTAINS(Email, "@yourcompany.com"))

Error Message:

"Email must be a company email address."


πŸ“ Tips for Writing Validation Rules

  • Use ISBLANK(), ISNULL(), ISCHANGED() to check data.

  • Use AND(), OR(), NOT() for combining conditions.

  • Test rules before making them active.

  • Use $User, $Profile, $Permission for user-specific logic.

  • Keep error messages clear and helpful.


πŸ§ͺ Testing Your Validation Rules

  1. Save and activate the rule.

  2. Go to a record and try to save invalid data.

  3. Make sure the error message appears as expected.

  4. Adjust logic if needed.


🚫 Common Mistakes to Avoid

  • Using wrong field names or labels.

  • Making the rule too restrictive.

  • Not adding exceptions for system users.

  • Forgetting to deactivate rules before data imports.

  • Writing unclear error messages.


πŸ”„ How to Edit or Delete a Validation Rule

  1. Go to the object in Object Manager.

  2. Click Validation Rules.

  3. Find the rule you want to change.

  4. Click Edit or Deactivate.


🎯 Best Practices

  • Name rules clearly (e.g., Amount_Positive_Check).

  • Write short and clear error messages.

  • Group related rules together for better maintenance.

  • Review regularly with business teams.

  • Use custom metadata or custom settings for dynamic values.


🧠 Final Thoughts

Validation Rules are a powerful way to:

  • Ensure clean, complete, and accurate data.

  • Enforce business processes.

  • Reduce user mistakes.

They’re easy to create but make a huge difference in data quality.


πŸš€ Start Using Validation Rules Today!

Whether you're a Salesforce Admin or Developer, mastering Validation Rules will make your applications smarter, safer, and easier to use.

Keep it simple, test carefully, and write clear rules!


Learn Salesforce Development Training Course

Read More





Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview