Order of Execution in Salesforce : Complete Guide

May 28, 2024
1474 Views
Order of Execution in Salesforce : Complete Guide

If you’re a Salesforce developer or administrator, knowing the order of execution is key to avoiding data errors, flawed logic, and slow performance.

When you save a record (insert, update, or upsert), Salesforce follows a structured sequence to keep data accurate.

This guide breaks down Order of execution in salesforce, why it matters, and how to optimize your automation strategies.

 

Understanding Salesforce Order of execution

Image Link – https://architect.salesforce.com/1/asset/immutable/s/e6cf2ac/assets/images/Salesforce-Order-Of-Execution-Diagram.png

Please note

that the diagram is specific to the API version specified. Refer to the Apex Developer Guide for the current order of execution.

Well, we have a couple of JavaScript validations running in the browser even before Salesforce fires server-side events.

What is Order of Execution in Salesforce?

Salesforce follows a predefined sequence of operations when processing DML (Data Manipulation Language) statements. Think of it as a step-by-step recipe that ensures validation rules, triggers, and workflows function correctly.

Without a clear understanding of this execution flow, developers may overwrite field values, create recursive loops, or cause automation conflicts.

Key Steps of Order of Execution in Salesforce

Here’s a detailed breakdown of the steps involved in Salesforce order of execution:

  1. Retrieve Record
    • Retrieves the existing record from the database or sets up a new record for an upsert operation.
  2. Update Record
    • Updates the record with new field values from the request, replacing the old values.
  3. Perform System Validation Checks (for standard UI edit page requests):
    • Compliance with layout-specific rules.
    • Ensures required field values are present.
    • Validates field formats and maximum length.
  4. Custom Validation Rules
    • Applies validation rules for User objects and other record types.
  5. Run Pre-Save Flows
    • Executes record-triggered flows that run before the record is saved.
  6. Execute Before Triggers
    • Executes all applicable before-save triggers.
  7. Perform Additional Validations
    • Rechecks required fields and custom validation rules.
  8. Execute Duplicate Rules
    • If a duplicate is detected and blocked, no further steps are taken.
  9. Temporarily Save Record
    • Data is temporarily stored in the database, but changes are not yet committed.
  10. Execute After Triggers
    • Executes all applicable after-save triggers.
  11. Execute Workflow Rules
    • Updates the record again if any workflow field updates are triggered, followed by revalidations.
  12. Run Post-Save Automations
    • Executes record-triggered flows, Process Builder processes, and other automation.
  13. Commit Changes to Database
    • Finalizes all DML operations.
  14. Execute Post-Commit Logic
    • Triggers actions such as sending emails, enqueuing asynchronous jobs, and executing entitlement rules.

Pro Tips for Developers and Admins

  • Understand Trigger Behavior:
    • Trigger.old does not reflect field updates made by workflow rules after a record update.
  • Handle Static Variables Cautiously:
    • Triggers can execute multiple times within a transaction. Static variables are not reset between executions.
  • Account for Roll-Up Summary Fields:
    • Roll-up summary field calculations propagate changes up the hierarchy (e.g., parent and grandparent records).

Practical Use Case: Why Order of Execution Matters

Imagine you’re building an automation to update an opportunity’s stage based on specific conditions. If you don’t account for the order of execution:

  • A before-save trigger may execute before a workflow updates a field.
  • This could result in incorrect logic execution or unexpected overwrites.

By understanding Salesforce’s execution flow, you can optimize your automation for better performance and reliability.

Key Points to Remember about Order of Execution in Salesforce

  • Trigger. old does not represent field updates that are made by workflow rules after the record is updated. It will contain field values before the initial update.
  • Once the DML operation is tried with partial success, triggers can be executed multiple times within the transaction. Between these trigger executions, static variables on classes accessed by those triggers are not reset.
  • Execution order is not guaranteed for multiple triggers on the same object for the same event type.
  • Inserting a non-private contact and then associating it with various accounts in multiple orders can be found on AccountContactRelation.
  • For details on using before triggers to set Stage and Forecast Category in opportunities, one should refer to the Opportunity section.
  • In API version 53.0 and prior versions, after-save record-triggered flows are executed after entitlement processing is done.

Final thoughts

In short, Order of Execution is fundamental for any developer or admin working in the Salesforce space. Understanding this flow guarantees that you develop applications and automation that work correctly.

Developers can build solid and reliable solutions by understanding how Salesforce processes records, including (but not limited to) validation rules, triggers, workflows, etc. Moreover, it’s also essential to be conscious of the possibility of future changes in each new release Because this is ultimately about ensuring that logic and data integrity remain reliable over time.

As a result, thorough knowledge of the Order of Execution strengthens existing operations and prepares you for flexibility in upcoming features. It’s evident that staying informed through official Salesforce documentation, such as the Apex Developer Guide, is imperative for continued success in the dynamic landscape of Salesforce development.

That’s why it is clear that in the rapidly evolving world of Salesforce development, you must remain current through well-documented resources like official Salesforce documentation / Apex Developer Guide.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Written by

Raja Patnaik

With over a decade of experience in Salesforce, I specialize in crafting smart solutions for industries like real estate, healthcare, banking, and payments. As director of Technology Architecture at RafterOne, I help businesses grow and streamline operations by building powerful B2B Commerce solutions on Salesforce. I’m passionate about turning ideas into reality—from gathering business needs to designing, developing, and launching solutions that create real value. I’ve had the opportunity to lead projects integrating tax, shipping, and payment systems into e-commerce platforms, improving workflows and customer experiences. A highlight of my work has been automating CPQ and billing processes using the Lightning B2B Store. I'm also honored to serve on the Salesforce B2B Commerce Partner Advisory Board, contributing to the evolution of future products. I love learning and staying ahead of the curve, focusing on solving complex problems and driving meaningful results for businesses.

Get the latest tips, news, updates, advice, inspiration, and more….

Contributor of the month
contributor
Abhishek Kolipey

Salesforce Evangelist | Certified Architect | Global Tech Speaker

Categories
...
Boost Your Brand's Visibility

Want to promote your products/services in front of more customers?

...

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Double Opt-in by Forge12