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
Manish Anand 10Manish Anand 10 

Order of execution of events in salesforce while saving a record.

Hi,

I am confused while it comes to order of execution of 'before triggers' and 'validation rules' while saving a record in salesforce. Salesforce document says-1)System Validation 2)Before Triggers 3)Custom Validation. I had this question in my developer exam and 2 options were confusing.
A)Before Triggers  B) Validation Rules,
Options were-1)AB 2)BA 
Which one of above is correct?

 
Best Answer chosen by Manish Anand 10
ManojSankaranManojSankaran
This is the order of execution.
1. Before Trigger
2. Validation Rule

Sorry wronly mentioned in the above post.

To be more detailed :
1. System Validation (Page lavel validations, field length checks)
2 . Before trigger logic (A)
3 . Custom Validation (B)


Mark this as answer if you agree

All Answers

ManojSankaranManojSankaran
Validation Rules mentioned in the question is a custom validation. Not a system validation (Pagelayout validations which includes mandatory check, field length).

so the order is B and then A - First Validation Rule and then Trigger.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
Manish Anand 10Manish Anand 10
You mean 1.Before Triggers 2. Validation , so A,B , right?
ManojSankaranManojSankaran
This is the order of execution.
1. Before Trigger
2. Validation Rule

Sorry wronly mentioned in the above post.

To be more detailed :
1. System Validation (Page lavel validations, field length checks)
2 . Before trigger logic (A)
3 . Custom Validation (B)


Mark this as answer if you agree
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Yes A,B is correct
Please check below post for order of execution
1) http://amitsalesforce.blogspot.in/2015/01/triggers-and-order-of-execution.html

1.     Loads the original record from the database or initializes the record for an upsert statement.
2.     Loads the new record field values from the request and overwrites the old values.
If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:
     Compliance with layout-specific rules
         Required values at the layout level and field-definition level
         Valid field formats
         Maximum field length
Salesforce doesn't perform system validation in this step when the request comes from other sources, such as an Apex application or a SOAP API call.
Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.
3.     Executes all before triggers.
4.     Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
5.     Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
6.     Saves the record to the database, but doesn't commit yet.
7.     Executes all after triggers.
 
Munira MajmundarMunira Majmundar
One thing on this topic is not clear to me.

The developer document on the Trigger and Order to Execution states:
Before Salesforce executes these events on the server, the browser runs JavaScript validation if the record contains any dependent picklist fields. The validation limits each dependent picklist field to its available values. No other validation occurs on the client side.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

I got it that the First action is performed on Client side - Browser runs Javascript validation and checks whether record contains any dependent picklist.  Fine.

BUT....

Why does the system check whether record contains any dependent picklist?
What does the system do if there is a dependent picklist?
What does the system do if there is NO dependent picklist?

In the same context, what does the following sentence even mean?
The validation limits each dependent picklist field to its available values.
huh????

Any help in clarifying the aforementioned conept will be highly appreciated.

Thanks,
Regards,
Munira
HowToDo ItInSalesforceHowToDo ItInSalesforce
Hi Techies,

I have listed down Order of execution in Salesforce, Check this out  Order of Execution in Salesforces in 15 Easy Steps (https://howtodoitinsalesforce.blogspot.com/2016/11/order-of-execution-of-records-in.html)

Thanks