Concepts or coding lessons of Salesforce that you can implement easily

Order of Execution in Salesforces in 15 Easy Steps

how to do it in salesforce
It's very important for any Salesforce Developer or Administrator to keep the order of events which Salesforce execute when any DML statement that is inserted, update, or upsert statement occurs on any record.

Following is an UPDATED Order of Execution in Salesforce:


1. Load Old and New values.

2. If the request came from a standard UI edit page, Salesforce runs system validation to check the record for: 
  a. Compliance with layout-specific rules.
  b. Required values at the layout level and field-definition level.
  c. Valid field formats.

  d. Maximum field length.

When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Before executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.

3. Executes "Before Save" Record Flows.

4. Executes all before triggers. After this, 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. then Custom Validation rules. 
6. Executes duplicate rules. If found duplicate record then stop execution.
7. Now Salesforce Save the record but doesn't Commit.
8. then Executes all after triggers.
9. then Executes assignment rules.
10. then Executes auto-response rules.
11. then Executes workflow rules. 
    If above workflow contains field updates, then 
    a. Updates the record again.
    b. Runs Standard Validation Again but No Custom Validation rules triggered.
    c. fires before and after triggers one more time .
12. Execute escalation rules. 
13. Executes Flow Automation in no particular order.
    a. Processes.
    b. Flows which are launched by Processes.
    c. Flows which are launched by Workflows.
14. Executes "After Save" Record Flows.
15. If the record is part of a cross-object workflow or it contains a roll-up summary field , then performs calculations and updates the roll-up summary field in the parent record. Also, the Parent record goes through save process.
16. If the parent record is updated, and it`s a grand parent record contains a roll-up summary field or is part of a cross-object workflow, then system performs calculations and updates the roll-up summary field in the parent record. Also, the Grand-parent record goes through save process.
17. Executes Criteria Based Sharing evaluation.
18. Commits all DML operations which are successful to the database.

19. Executes all post-commit logic like Sending an email to users.

If you aware of all above sequences then it will help you configure your apps correctly and troubleshoot.

Additional Considerations:


Please note the following when working with triggers.

  • If your org contains multiple triggers for the same object to the same event, then the order of execution isn’t guaranteed.
    For example, if you have three before insert triggers for Contact, and a new Contact record 
         is inserted that fires the three triggers, you can not guarantee the order in which these triggers fire.
  • When your apex code contains DML and DML call is triggered with partial success, if the initial DML attempt results in errors for some records then more than one attempts are executed to the saved the successful records.
         For example, an error can occur for a record when a user-validation rule fails. Triggers are fired during the first attempt and are fired again during subsequent         attempts. Because these trigger invocations are part of the same transaction, static class variables that are accessed by the trigger aren't reset.
  • Before the specific update that fired the trigger, Trigger.old contains a version of the objects. But, there is an exception. When a record is updated and subsequently triggers a workflow rule field update, Then the Trigger.old which is in the last update trigger contains the version of the object before the initial update was made. It won`t contains immediately prior to the workflow update, but the object before the initial update was made.
         For example, suppose an existing record has a text field with an initial value of Nitish.
         A user updates this field to ABC, and a workflow rule field update fires and update that filed to XYZ. 
In the update event of trigger that fires after the workflow field update, the text field value of 
         the object obtained from Trigger.old variable is the original value of Nitish, instead ABC, 
         as would typically be the case.

If you are running into issues where the system is not behaving the way you expect it to, the best way to diagnose and troubleshoot will be to turn the debug on.

To set debug log follow below steps
  • Go to Setup
  • Then go to Monitor
  • Expand Logs
  • And then select Debug logs.
  • Click on New button
  • Select User from user lookup for which you have to capture the debug logs
  • Select start date and end date for debug. (You can select maximum 1 day for per user)
  • Select the debug level from lookup. You can create new debug level with appropriate filters.
  • Click Save button.
For simplification I can say it as Go to Setup -> Monitor -> Logs -> Debug Logs

Now execute the transaction and then check the debug log.

Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next post: Simplest Way To Get Current User Profile In Apex Class 


Latest Salesforce Interview Questions and Answers:



More Salesforce Blogs:

Difference Between Indirect Lookup and External Lookup In Salesforce
What is the use of nextStartDate in Salesforce?
Check out Salesforce Daily Limit Only in 5 Simplest Steps
Learning Pagination In Salesforce Is Not Difficult At All ! You Just Need 3 Easy Steps
How To Learn Get Field Values From Visualforce Page To Apex Class Controller Without Losing Your Mind
Main Difference Between ISBLANK And ISNULL in Salesforce
How To Get Total Amount Of Records Processed In Batch Job In 10 Minutes And Still Look Your Best 
Export VisualForce Data into Excel Sheet in 3 Easiest Steps
7 Easy Steps to Generate Apex Class From WSDL In Salesforce
Simplest Way To Find Number of Days Between Two Dates in Salesforce
3 Easy Steps To Send Emails With Attachment From Your Apex Class In Salesforce
How Insert Comma In Formula Fields Can Help You Improve Your Productivity
Simple Guidance For You In Access Of Subquery Field Value Using Apex - Upwards Traversal.
Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal


How Learning Enable Inline Editing In Visual Force Pages Could Save Your Money And Time
loading...

No comments:

Post a Comment