What is the Salesforce order of execution?
Salesforce order of execution refers to the sequence of different logic that is executed when creating, updating, or deleting a record. Salesforce has a well-defined sequence of events that occur during the processing of these actions to ensure data consistency and maintain the platform’s integrity.
However, before Salesforce executes the steps, the browser performs JavaScript validation to determine whether the record includes any dependent picklist fields. This validation restricts each dependent picklist field to its valid values. That’s the only validation on the client side.
Components of the order of execution
Salesforce’s order of execution involves various components and phases that determine the sequence in which operations, triggers, processes, and automation execute. The key components of Salesforce’s order of execution include:
- Record initialization: Salesforce loads the original record from the database or prepares a new record for an upsert operation (when rows are inserted into a database table if they do not already exist or updated if they do).
- Field value update: New field values from the request overwrite the old values.
- System validation rules: These rules enforce fundamental data integrity checks such as required fields, valid field format, maximum field length, and unique field values. In other cases, it validates only foreign keys and checks for self-referencing custom foreign keys. If multiline items exist (e.g., quote line items), Salesforce runs custom validation rules.
- Before record triggered flows: Salesforce allows you to leverage Flow Builder to declaratively create automated business logic using clicks and not code. Before record triggered flows execute before saving a record.
- Run all before triggers: Before triggers are Apex code that executes before records are saved to the database. Use before triggers to modify record data before it’s committed.
- Validation recheck: System validation repeats for required fields with non-null values. Custom validation rules are executed again.
- Duplicate rules: Duplicate rules prevent the creation of duplicate records.
- Record save: The record is saved but not committed to the database.
- Run all after triggers: An after trigger executes after any change to a record, including changes made by other triggers. For example, you could use an after trigger to automatically update related records when a record changes.
- Assignment rules: Assignment rules assign ownership of records (e.g., leads for prospective customers, or cases for after-sales service) to specific users or queues based on predefined criteria.
- Auto-response rules: Auto-response rules send automated email responses to prospects or customers based on specific conditions.
- Workflow rules with field updates: Workflow rules automate internal processes by defining criteria and executing specified actions, such as updating fields or sending email alerts. Both before and after update triggers will execute again but for the final time, irrespective of the insert or update operation on the record.
- Escalation rules: Escalation rules automatically escalate cases to higher levels of support depending on the criteria.
- Processes and flows: Processes and flows automate actions based on specified criteria. They can include both auto-launched and record-triggered processes. Processes and flows execute after before triggers.
- Entitlement rules: Entitlement rules outline the steps needed to resolve a case.
- Roll-up summary fields (parent and grandparent): If applicable, these are used to calculate and update values from related records, such as those in parent and grandparent records.
- Criteria-based sharing evaluation: Sharing rules are evaluated based on criteria.
- Database commit: All DML operations, including inserts, updates, and deletes, are committed to the database.
- Post-commit logic: Executes post-commit logic after saving changes, such as sending emails and executing asynchronous Apex jobs (e.g., queueable jobs and future methods).
You should note that steps from assignment rules to the roll-up summary field are skipped during a recursive save.
Salesforce order of execution and record locking
As various processes, triggers, and automation execute during record operations, record locking ensures that multiple users or processes don’t modify the same record simultaneously. This is because it could lead to data conflicts and inconsistencies.
When Salesforce processes updated records according to the order of execution, it takes record locking into account to prevent data conflicts. For example, when a before trigger modifies a record, it locks the record. As a result, other triggers, workflows, or processes only execute after the before trigger. Record locking ensures data consistency and prevents race conditions.
When Salesforce processes updated records according to the order of execution, it takes record locking into account to prevent data conflicts.
Troubleshooting and debugging order of execution issues
Troubleshooting order of execution in Salesforce helps ensure your automation, triggers, processes, and workflows behave as expected. Below are some steps to take when troubleshooting.
Enable debug logs
Salesforce provides debug logs that capture detailed information about the order of execution for a specific transaction. Review the debug logs to see which processes, triggers, and validations are firing and what data they are processing. Look for error messages or unexpected behavior.
Analyze error messages
If there are error messages in the debug logs or on the user interface, carefully analyze them. Error messages often provide clues about what went wrong. Pay attention to custom validation rule errors, trigger failures, or workflow rule errors.
Review trigger logic
Examine the logic within your triggers, processes, and workflows. Correctly define conditions and sequences. Check for any logical errors, infinite loops, or recursion in your Apex triggers.
Consult Salesforce community about order of operation issues
If you cannot identify the issue independently, consider seeking help from the Salesforce Trailblazer community or Salesforce support.
Best practices
Best practices for managing Salesforce’s order of execution involve optimizing your processes, triggers, and automation to ensure efficient and reliable data handling. By adhering to these practices, you can avoid conflicts, maintain data integrity, and create a more streamlined user experience.
Plan your process logic carefully
Before implementing automation, thoroughly plan the logic of your processes, triggers, and workflows. Consider the sequence of events and the impact of each action on the data. This proactive approach minimizes conflicts and unexpected outcomes.
Use bulk processing techniques
Design your triggers and processes to handle multiple records simultaneously (bulk processing). This reduces the number of times triggers and processes fire, optimizing performance, and reducing the potential for record locking.
Minimize trigger logic
Keep trigger logic focused on specific tasks and avoid unnecessary complexity. Splitting triggers based on actions (e.g., before insert, before update) can help maintain clarity and manage the order of execution.
Limit the use of recursive triggers
Recursive triggers occur when a trigger update causes another trigger to fire, leading to a loop. Use design patterns like a static variable to prevent unnecessary recursion.
Prioritize record locking best practices
Structure your automation to minimize conflicts by locking records for the shortest duration possible. Use FOR UPDATE SOQL queries to explicitly lock records when needed, ensuring that your code is aware of and respects record locks. Avoid long-running or complex operations within locked record contexts.
Structure your automation to minimize conflicts by locking records for the shortest duration possible.
Thoroughly test your automation
Before deploying automation to production, thoroughly test its behavior in a sandbox or development environment. This testing helps uncover issues related to the order of execution, logic conflicts, and performance.
Stay updated with Salesforce releases
Stay informed about new Salesforce releases and changes to the order of execution. Salesforce’s updates can introduce changes that affect your existing automation, so staying current is critical.
Wrapping up
To ensure your Salesforce automation aligns with the platform’s order of execution, careful planning and continuous testing are crucial. Testim Salesforce offers a powerful solution for automating these tests with its AI-driven platform, designed to adapt to Salesforce’s complexities. By simplifying the creation and maintenance of automated tests, Testim helps prevent conflicts, maintain data integrity, and optimize the efficiency of your Salesforce processes. Consider exploring Testim’s capabilities through their free trial to enhance the quality of your Salesforce automation.
This post was written by Mercy Kibet. Mercy is a full-stack developer with a knack for learning and writing about new and intriguing tech stacks.