Process Automation, Logic, and Integration27% of the exam
Advanced trigger context and handler patterns
Which trigger context variables exist in each event, how merge and undelete fire triggers, how addError blocks saves and deletes, and how a single trigger with a handler, bulk-safe code and Id-based recursion guards survives 200-record chunks.
Demonstrate knowledge of the best practices for writing Apex triggers.
~7 min read · 10 practice questions ·
What the exam expects
- Read trigger code and spot a context variable used in an event where it is null or read-only (
Trigger.newin delete,Trigger.newMapin before insert, editingTrigger.newafter save). - Route logic to the exact event with
Trigger.operationTypeinstead ofisInsertalone. - Predict which triggers fire on merge and undelete, and where
MasterRecordIdis readable. - Block a save or delete with
addError()on the right collection. - Apply the trigger standards: one trigger per object with handlers, bulk-safe SOQL and DML, asynchronous callouts, and recursion guards that work across 200-record chunks.
Key terms: context variable (an implicit System.Trigger member such as new, oldMap or operationType); handler (the class a trigger delegates to); recursion guard (state that stops logic re-running in one transaction); chunk (a 200-record batch that gets its own trigger invocation); losing record (a record deleted by a merge).
Key facts
Trigger.new
- Detail
- Insert, update and undelete only; editable only in before triggers
Trigger.newMap
- Detail
- Before update, after insert, after update, after undelete
Trigger.old and oldMap
- Detail
- Update and delete only; always read-only
DML on trigger collections
- Detail
- Trigger.new and Trigger.old can't be used in DML
Merge
- Detail
- Delete events for losers, update events for the winner; no merge event
MasterRecordId
- Detail
- Set on losing records only in after delete
addError in delete triggers
- Detail
- Call it on Trigger.old records
Chunk size
- Detail
- Trigger invoked per 200-record batch; Trigger.size counts the batch only
Between Bulk API chunks
- Detail
- Limits reset; static variables don't
DML statement limit
- Detail
- 150 per transaction
Sources for this section (6)
- Trigger Context Variablesdeveloper.salesforce.com (opens in a new tab)
- Context Variable Considerationsdeveloper.salesforce.com (opens in a new tab)
- Triggers and Merge Statementsdeveloper.salesforce.com (opens in a new tab)
- Trigger Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Static Variablesdeveloper.salesforce.com (opens in a new tab)
- Governor Limitsdeveloper.salesforce.com (opens in a new tab)
- Why does `Trigger.newMap` throw a NullPointerException in a before insert trigger?
- When can a trigger read a losing record's MasterRecordId?
- Why does a static Boolean guard skip records after the first 200 in a Bulk API load?
- Which collection do you call addError() on to block a delete?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (10 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams