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:
- System validation — required fields, field-level data types, and basic integrity checks.
- Before triggers — custom Apex logic that runs before the record is saved (a full Triggers module covers this soon).
- Custom validation rules — declarative rules configured in Setup.
- The actual save — the record is written to the database, and
Idis assigned. - After triggers — custom Apex logic that runs after the record is saved, now with access to the final
Id. - 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.
What Really Happens When You Insert a Record Quiz
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.