As per Gartner, revenue in the Enterprise Software segment is expected to reach more than US$230 million by the end of 2021. Businesses are shifting more and more towards technological CRMs to keep up in today’s tech era.
Salesforce has a bunch of rules that can be defined on objects and fields. For example, you can define validation rules, escalation rules, auto-response rules, triggers, workflow rules, and different types of flows in Salesforce to automate business processes. These automation capabilities empower users to streamline and optimize their workflows, ensuring efficient data management and enhanced user experience.
If you are a Salesforce consultant, developer, or architect, it’s essential for you to understand the order in which the rules and triggers are executed. Let’s see the order of execution in Salesforce.
Watch this Salesforce Training video to learn more about salesforce concepts.
Order of Execution in Salesforce Explained
Order of Execution in Salesforce is nothing but a set of rules that govern, or describe the path of a record while it travels through multiple automation and events happening from SAVE to COMMIT. In simpler terms, when you save a record by giving the insert, update, or upsert statement, Salesforce performs certain events in an orderly manner. This is termed the order of execution in Salesforce. The order of execution starts with a DML operation and later there are further involvements like triggers, out-of-the-box Salesforce components, workflows, different rules, etc.
Steps of Order of Execution in Salesforce
The sequence of steps mentioned below outlines the order of execution in Salesforce when a record is being processed and saved. Each step occurs in a specific order, and the execution order is crucial for maintaining data integrity and ensuring that various validations, rules, and automations are applied correctly. The order of execution is as follows:
- Load the original record from the database and load the record for the upsert statement.
- Load the new record field values from the request and overwrite the old values.
If the request comes 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
- Whenever a request comes from other sources, e.g., Apex application or a SOAP API call, Salesforce validates only the foreign keys.
- Executes record-triggered flows that are configured to run before the record is saved.
- Executes all before triggers.
- Run most system validation steps. Verify that all required fields have a non-null value and run user-defined validation rules.
- Executes duplicate rules.
- Saves the record to the database, but doesn’t commit yet.
- Executes all after triggers.
- Executes assignment rules.
- Executes auto-response rules.
- Executes workflow rules.
- Executes escalation rules.
- If there are workflow field updates, update the record again.
- If the record was updated with workflow field updates, fires before update triggers and after update trigger one more time (and only one more time), in addition to standard validations.
- Executes processes and flows launched using flow trigger workflow actions.
- Executes entitlement rules.
- Executes record-triggered flows that are configured to run after the record is saved.
- If the record contains a roll-up summary field or is part of a cross-object workflow, perform calculations and update the roll-up summary field in the parent record. Parent record goes through the save procedure.
- If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record.
- Executes Criteria Based Sharing evaluation.
- Commits all DML operations to the database.
- Executes post-commit logic, such as sending an email.
After Commit Logic: Actions in the After-Data-Change Phase
The actions mentioned below are part of the broader “after commit logic” that takes place once the data changes are permanent after the order of execution.
- All emails are sent
- Asynchronous apex: @future
- Processes Async Sharing Rule
- Queues outbound messages
- Calculates index
- Renders previews for files
- If Platform Events are configured, they are published
Triggers in Salesforce
The two types of Triggers in Salesforce are as follows:
- Before Triggers – These are executed before the record values are saved to the database.
- After Triggers – These are used to trigger records that are read-only.
Before or after applying changes to the Salesforce record, you can make customized changes to it. This is enabled by triggers. Triggers can invoke apex. Triggers are executed before or after these operations:
- undelete
- upsert
- merge
- delete
- update
- Insert
Some top-level objects like Account, support triggers. You can define triggers for them. If you want to define a trigger, you can go to that particular object Object Management Settings and go to Triggers.
Get 100% Hike!
Master Most in Demand Skills Now!
Which Operations Don’t Invoke Triggers?
The steps listed below are related to various scenarios and conditions that can impact the execution of triggers in Salesforce. While they do not follow a specific sequence like the order of execution during record processing and saving, they outline different situations in which triggers may or may not be invoked. These scenarios involve specific conditions or actions that might affect the behavior of triggers.
- All the records that didn’t start a delete, don’t invoke triggers
- Child records that have been reparented using the merge operation
- Huge email actions, division transfers, address updates
- Custom field data being modified
- When you rename or replace a picklist
- When you change a user’s division that is the default, while the transfer division option is checked
- Price book management
- Update account trigger won’t work before/after a user’s account is changed to a business one
- When the LikeCount counter increases, update triggers won’t work on FeedItem
When you have multiple triggers for the same object due to the same event, the execution of that trigger is not guaranteed. You’ll need to implement Trigger Framework in this case.
Conclusion
As we have discussed in order of execution there is a lot more to understand than triggers run before workflows. Understanding the flow and apps will have more scalability and reliability.