The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

How the Salesforce order of execution sequences flows, triggers and validation (opens in a new tab)

You will learn the rough sequence in which Salesforce runs Flows, Apex triggers, Validation Rules and after-save automation when a record is saved, and why that order matters when several automations touch the same record.

Why read this

The article is a general overview rather than a detailed reference.

  1. Initial record submission
  2. Before-save Record-Triggered Flows
  3. Before Apex triggers
  4. Validation and duplicate rules
  5. Save to database, not yet committed
  6. After triggers and after-save flows
  7. Commit and post-commit operations
The main stages Salesforce runs when a record is saved

When a record is created, updated or deleted, Salesforce runs several kinds of automation in a defined sequence. The article says this matters because multiple automations touching the same record can produce unexpected results, repeated updates or errors. Understanding the sequence helps teams troubleshoot and avoid needless complexity.

The process starts with the initial submission, whether from the user interface, the API, Apex, Data Loader or an external application. Before-save Record-Triggered Flows then run before the record is written to the database, and are described as efficient for simple field updates because they avoid an extra save. Before Apex triggers follow, with a short example that sets a default value on a Contact description. Validation Rules and duplicate rules run before the save completes.

Once the record is saved to the database, the transaction is not yet committed, so later errors can still roll the changes back. After triggers and after-save flows then run, handling tasks such as creating related records, sending notifications or starting further processes. The article also notes that legacy workflow, escalation and assignment rules can cause extra updates that re-run parts of the sequence. After a successful transaction, the changes are committed and post-commit work such as Queueable Apex, future methods and scheduled paths can run.

The article closes with common mistakes: several Flows on one object without reviewing their order, duplicating field updates across Flows and Apex triggers, ignoring recursion, and skipping tests for API updates. Its advice is to review existing automation on an object before adding a new Flow, trigger or Validation Rule.

Read the original at dev.to (opens in a new tab)

Worth reading?

Must-read

Why a static Boolean Trigger guard silently skips records in bulk (opens in a new tab)

You will learn why a static Boolean recursion guard fails when a DML statement touches more than 200 records, how to reproduce the bug with a test, and how to replace it with an Id-based Set or a change-based check.

Static Boolean guard

  • One flag for the whole transaction
  • Chunk 2 onward is skipped
  • 800 of 1,000 records unprocessed
  • Insert still succeeds silently

Id-based Set guard

  • Tracks each record Id
  • New Ids in each chunk processed
  • Recursive Ids already in set skipped
  • Guards afterUpdate, not inserts
A static Boolean guard against an Id-based Set in a bulk trigger

Many Apex trigger tutorials recommend a static Boolean flag to stop a trigger from running again during recursion. The article argues this flag can silently skip records in production. Salesforce splits any DML statement touching more than 200 records into chunks of 200, running the trigger once per chunk within the same transaction. A static variable persists across those chunks, so the guard cannot distinguish a recursive call from the next batch of records, and records beyond the first chunk never get processed, though the insert still succeeds.

The problem is most likely in Batch Apex with a scope above 200, data migration scripts run from Anonymous Apex, service classes that insert or update large lists, and Flows that change many records in one element. It rarely appears in UI testing because records are saved one at a time. The article provides a test that inserts 201 Accounts and asserts that all 201 are processed. With the Boolean guard the test fails, reporting 200 processed, while a bulk-safe test catches the defect before release.

The recommended fix is to track record Ids in a Set rather than a single Boolean. Adding each Id to the set returns false when it was already present, so only genuinely new records are processed in later chunks, while a true recursive update is skipped. The article notes practical details: guard afterUpdate rather than inserts because records in before insert have no Ids yet, keep separate sets per event if both beforeUpdate and afterUpdate do work, and accept that a record updated twice on purpose is handled only once.

Often the better solution is to avoid needing a guard. Comparing field values against Trigger.oldMap and acting only on the change of interest stops recursion naturally, because the follow-up update does not change the field again. A before-insert handler that only sets fields on Trigger.new cannot cause recursion and needs no guard. The article closes with a checklist: search the org for static Boolean flags in trigger handlers, remove guards that are unnecessary, react to changes rather than every update, use Sets where a guard remains, and test every trigger with 201 or more records in one statement. The article also promotes a commercial starter kit, which is not part of the technical guidance.

Read the original at dev.to (opens in a new tab)

Also told by dev.to (opens in a new tab), dev.to (opens in a new tab)

Worth reading?

Must-read

Bulkifying an Opportunity trigger to avoid the 101 SOQL limit error (opens in a new tab)

You will learn why a per-record SOQL query inside a trigger loop fails on bulk loads, and how to refactor it to one query and one DML statement using a map keyed by Account ID.

Why read this

You will also see the trade-offs between triggers, Flows and Queueable Apex.

Per-record (fails)

  • Query Account inside the loop
  • Update Account inside the loop
  • Fails on record 101

Bulk-safe (map)

  • Collect deltas in a map by AccountId
  • One SOQL query with IN
  • One DML update for the list
The failing per-record pattern set against the bulk-safe pattern

A trigger that passes a single-record test can fail a 200-row bulk import with a Too many SOQL queries error. This happens with Data Loader, Bulk API and scheduled integrations, and it usually shows up first in production, because sandbox test data rarely reaches 200 rows per batch. The article argues that the fix is to change how the trigger reads and writes data, not to retry or buy more licences.

The author uses an after update trigger on Opportunity that adds the Amount of newly closed-won deals to a Total_Won__c field on the parent Account. The original version queries and updates the Account once per record, which breaks on record 101 under the 100 SOQL limit and would also run into the 150 DML statement cap. The article uses this failing version to show the cause of the error.

The refactor has three steps. First, audit every query and DML statement that sits inside a loop over Trigger.new. Second, collect the qualifying deals into a map keyed by AccountId, filtering on the stage change so later edits do not double count, then run a single query with IN and a single update. The map also merges several deals for the same Account. Third, weigh the options: a handler class gives testability and bulk control, Record-Triggered Flow is quicker for simple logic but shares the same caps, and Queueable Apex allows more queries and CPU time at the cost of eventual consistency.

The article closes with a short FAQ. It notes that a LimitException cannot be caught with try-catch, that Limits.getQueries() can be used to check usage, and that Queueable suits follow-up work while Batch Apex suits large record sets. It recommends testing with 200 records in a single call so that the test matches the real chunk size.

Read the original at dev.to (opens in a new tab)

Worth reading?

Must-read

Testing Salesforce Flows locally with renmy runtime before activation (opens in a new tab)

You will learn how to run Record-Triggered Flows and Apex triggers on a local machine, catch order-of-execution overlaps before activation, and what the runtime does not cover.

  1. Retrieve flow into project folder
  2. Write test creating and closing record
  3. Run renmy test locally
  4. Fix overlap, rerun in seconds
  5. Activate and review in sandbox
A local test run replaces repeated sandbox rounds for checking flow and trigger behaviour

renmy runtime, formerly called Nimbus until its October 2026 rename, is a local Salesforce Apex runtime. It executes Apex, SOQL, DML and Salesforce Flows (Autolaunched, Record-Triggered and Screen Flows) on a developer’s own computer against an embedded database, with no org, sandbox or Scratch Org involved. The article argues that testing flows in a shared sandbox is slow and risky: other people’s automation fires alongside yours, test data disappears after refreshes, and overlaps between a flow and an Apex trigger on the same object often surface only after activation. Running locally, a Record-Triggered Flow fires together with the object’s triggers in the platform’s order of execution, so overlaps show up in milliseconds.

The article walks through an example: a Record-Triggered Flow that creates a follow-up Task when an Opportunity is set to Closed Won, alongside an older Apex trigger that also creates a Task. A developer retrieves the flow file into the project folder with the Salesforce CLI. They then write a short Apex test that creates an Account and an Opportunity, moves the stage to Closed Won, and asserts that exactly one Task exists with the expected subject. The admin can read that test as an acceptance criterion. Running renmy test with the test name, or with “” for every test, reports the result. If the trigger also creates a Task, the assertion fails with two Tasks, revealing the overlap. The fix is either to remove the trigger’s logic or to add an entry condition to the flow, and the rerun takes about a second. Once the flow is ready, renmy deploy runs the local checks before anything reaches the org.

The same runtime is available through a browser-based Dev UI started with renmy dev, which offers a test runner, schema browser and coverage explorer. A VS Code extension adds run buttons above each test, and a JetBrains plugin does the same in IntelliJ IDEA, including its Community edition, and can display a flow as a graph. The article is clear about the limits. The runtime executes a Screen Flow’s logic but does not render its screens, so layout, labels and conditional visibility must be checked in a sandbox with a user. It reproduces platform rules closely but is not Salesforce itself, and permissions, page layouts and user acceptance still require an org. The Free tier includes unlimited local test runs on one machine, with no account or card required.

Read the original at dev.to (opens in a new tab)

Worth reading?