Intermediate 25 min read

What Really Happens When You Insert a Record

By the end of this lesson, you'll be able to:

  • Name the major stages Salesforce runs through during a single insert
  • Explain where triggers and validation rules fit into that sequence
  • Recognize why this sequence matters for understanding "order of execution" bugs later

Prerequisites: "Transactions and Savepoints"

The simplified sequence

When insert acc; runs, Salesforce works through roughly this sequence, in order:

  1. System validation — required fields, field-level data types, and basic integrity checks.
  2. Before triggers — custom Apex logic that runs before the record is saved (a full Triggers module covers this soon).
  3. Custom validation rules — declarative rules configured in Setup.
  4. The actual save — the record is written to the database, and Id is assigned.
  5. After triggers — custom Apex logic that runs after the record is saved, now with access to the final Id.
  6. Other automation — assignment rules, workflow rules, and similar declarative automation.

This is a simplified version — the full, exact order of execution (with rollback behavior and more edge cases) is a well-known and more advanced Salesforce topic, revisited directly once triggers are introduced.

Why this sequence explains a subtle behavior

Account acc = new Account(Name = 'Riverbend Farms');
System.debug(acc.Id); // null — nothing saved yet

insert acc;
System.debug(acc.Id); // now populated

acc.Id only exists after step 4 in the sequence above — this is exactly why a before trigger (step 2) never has access to a record's Id on insert, while an after trigger (step 5) always does. Nothing mysterious is happening — it's a direct consequence of when each step actually runs relative to the save itself.

Why this matters going forward

Almost every confusing "why did my trigger not see the field I just set" or "why is this validation rule blocking a normal update" question later in this course traces back to exactly where in this sequence a particular piece of logic ran. Having a rough mental map of the sequence now — even before triggers are formally introduced — makes those future lessons click much faster.

Exercise

As a comment, explain why a before-insert trigger never has access to the record's Id, while an after-insert trigger always does.

Show hint

Think about which step in the sequence assigns the Id.

APEX

What Really Happens When You Insert a Record Quiz

1. In the simplified insert sequence, what happens immediately before the actual database save?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A single insert statement triggers a whole sequence of platform steps behind the scenes — understanding this sequence is what makes later "order of execution" questions (like trigger timing) make sense instead of feeling mysterious.