function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChinnoyChinnoy 

DIfference Between Triggers and Validation rules?

Hi,

Can anyone clarify me When to use Triggers and When to use Validation Rules?

Explain with scenario is Appreciable...
Best Answer chosen by Chinnoy
VineetKumarVineetKumar
Validation Rule : Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.
Eg : A very basic validation for start date and end date, a start date cannot be greater than the end date, this can be handled using the validation rule, where you specify the formulae as (StartDate > EndDate) - Display Error ("Start Date cannot be greater than End Date"). Your record will not be saved unless you your formulae becomes false.
Validation rules apply to new and updated records.
You cannot perform and DML operation and many other things that a trigger can do.

Trigger : Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger can be used to perform some update on the same record or a related record based on some business criteria, can be used to perform DML operation on other records that meet some business criteria, can be used to send email, invoke a future HTTP callout etc.
Eg : A typical use of trigger would be to say update a contact field on Account, whenever a new Contact is inserted. To meet this requirement, you will write a trigger on contact, which will perform an update on the associated Account.

At the end it would depend on your actual requirement, just to check if the data entered is valid, use validation rule, to perform other complex business operations, use trigger.

Let me know if that helped.

All Answers

Vasani ParthVasani Parth
Venkat,

validation rules: Validation rules are used to confirm that the data entered into a record meet various data quality / business rules before letting the user save it.

Triggers: Triggers are pure custom coding(Apex) to create solutions for different business problems.it  can be used for various different things and can be executed at different times. e.g. when initiating a new record, before saving it, when a record is deleted.

In general, I can say validation rule is just blocking invalid data based on business rule, a trigger can do the same thing, but can do more.

Please mark this as the best answer if this helps
VineetKumarVineetKumar
Validation Rule : Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.
Eg : A very basic validation for start date and end date, a start date cannot be greater than the end date, this can be handled using the validation rule, where you specify the formulae as (StartDate > EndDate) - Display Error ("Start Date cannot be greater than End Date"). Your record will not be saved unless you your formulae becomes false.
Validation rules apply to new and updated records.
You cannot perform and DML operation and many other things that a trigger can do.

Trigger : Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger can be used to perform some update on the same record or a related record based on some business criteria, can be used to perform DML operation on other records that meet some business criteria, can be used to send email, invoke a future HTTP callout etc.
Eg : A typical use of trigger would be to say update a contact field on Account, whenever a new Contact is inserted. To meet this requirement, you will write a trigger on contact, which will perform an update on the associated Account.

At the end it would depend on your actual requirement, just to check if the data entered is valid, use validation rule, to perform other complex business operations, use trigger.

Let me know if that helped.
This was selected as the best answer
Abhishek Singh 88Abhishek Singh 88
Basic Purpose of validation rule is to display the error massege if some condition become true.
But Trigger can be used for many purpose it can display error,can popullate fields before or after event take place.
In short you can say:Trigger is combination of workflow and validation rule,and have few more feature.
 
DeepthiDeepthi (Salesforce Developers) 
Hi Venkat,

Validation rules are used to confirm that the data entered into a record meet various data quality / business rules before letting the user save it.  Triggers can be used for various different things and can be executed at different times - e.g. when initiating a new record, before saving it, when a record is deleted.  Validation rules are very easy to create and virtually anyone can learn how to create these.  Triggers are more complex and generally require some development skills.

In nutshell,
Trigger:- It is used for validating, insertion, updating, delete the record and it can execute for before insert,after insert,before update, after update, before delete, after delete,after undelete.
Validation rule:- It is used only validate the record a/c to the specified criteria.It works only when record is created, edited.
 
Hope this helps you!
Best Regards,
Deepthi
Gary RalstonGary Ralston
Hello Chinnoy,

Triggers and validation rules are both used in Salesforce to enforce business rules and data quality, but they serve different purposes.
  • Triggers are Apex code that execute before or after specific data manipulation language (DML) events occur. These events can be related to creating, updating, deleting, or undeleting records in Salesforce. Triggers are typically used to automate business processes, such as sending email alerts, updating related records, or performing complex calculations.
  • On the other hand, validation rules are a declarative tool that allow administrators to set specific criteria that must be met before a record can be saved. Validation rules are used to ensure that data meets certain standards, such as required fields, formatting, or data type constraints. When a user attempts to save a record that violates a validation rule, an error message is displayed and the record is not saved.
For a comprehensive guide on Salesforce triggers, I recommend checking out the article at https://arrify.com/triggers-in-salesforce/. It covers everything you need to know about triggers in Salesforce.