Master Order of Execution in Salesforce : Complete Guide

Nullam vel lectus vel velit pellentesque dignissim nec id magna. Cras molestie ornare quam at semper. Proin a ipsum ex. Curabitur eu venenatis

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

If you are a Salesforce developer or administrator, having detailed insights about the order of execution in Salesforce is essential. Not understanding the sequence of events that occur on servers once a record is “saved” (inserted, updated, or upserted) can lead to undesired data modifications and flawed logic execution, ultimately resulting in inaccurate results.

In this blog, we will delve into the Salesforce Order of Execution, its steps, and why it matters for developers and admins.

 

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 specific set of steps when saving a record, referred to as the Salesforce order of execution. Think of it as a recipe that ensures data integrity and smooth application functionality. Understanding this sequence is critical for avoiding errors and building robust solutions.

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:

Let’s say you’re building an automation to update an opportunity’s stage based on certain criteria. Understanding the order of execution ensures your before-save trigger executes correctly before workflow field updates modify the same record.

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.

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.

Leave a Reply

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

Contributor of the month
contributor
Mithun Naik

Software Engineer | Salesforce | 3X Certified Developer | Salesforce Blog Writer

Categories

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

...
Boost Your Brand's Visibility

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

...
0%
Think you know Salesforce?
Fill in the details to Get Started

A Salesforce Administrator has 7 million records that need to be loaded into Salesforce and wants to do it in one batch. How can the records be uploaded in one batch?

If a user is working in Salesforce when the login hours end, what will happen?

What is true about dynamic dashboards?

Which of the following can a Case Queue be used for?

Which of the following statements are true about resetting passwords when users get locked out of a Salesforce org that does NOT have single sign-on enabled through an identity service other than Salesforce?

Your score is

0%

This will close in 0 seconds