Process Automation and Logic28% of the exam
Apex class and trigger best practices
Write triggers the way the exam expects: context variables, one trigger per object with a handler class, bulkified SOQL and DML, before-trigger field updates, addError, change detection with Trigger.oldMap, recursion guards and no hardcoded configuration.
Given a scenario, follow best practices to write Apex classes and triggers.
~7 min read · 16 practice questions ·
What the exam expects
- Read trigger context variables and know which exist, and which are writable, in each event.
- Consolidate logic into one trigger per object that dispatches to a handler class.
- Bulkify: no SOQL or DML inside loops; sets, maps and one DML on a collection.
- Choose a before trigger for same-record field updates and
addError()to block a save. - Detect real field changes with
Trigger.oldMapand stop unwanted re-entry with a guard. - Keep configuration such as record type Ids out of code.
Key terms: context variable (an implicit Trigger member such as Trigger.new), handler class (the class a thin trigger delegates to), bulkification (code that works for 1 or 200 records within limits), recursion guard (logic that stops a trigger re-running its work in the same transaction).
Key facts
Trigger.new editable
- Detail
- Only in before triggers; after triggers throw a runtime error
Trigger.old and oldMap
- Detail
- Only in update and delete triggers
Trigger.newMap
- Detail
- Before update, after insert, after update, after undelete
Trigger batch size
- Detail
- 200 records per invocation; Trigger.size is the current batch
Multiple triggers, same object and event
- Detail
- Execution order not guaranteed
Limits a loop breaks
- Detail
- 100 synchronous SOQL queries, 150 DML statements
addError from an Apex DML statement
- Detail
- One error rolls back the whole operation
Static variable scope
- Detail
- One transaction; persists across trigger invocations in it
Sharing keywords on triggers
- Detail
- Not allowed; triggers typically run without sharing
Sources for this section (7)
- Context Variable Considerationsdeveloper.salesforce.com (opens in a new tab)
- Trigger Context Variablesdeveloper.salesforce.com (opens in a new tab)
- Record-Triggered Automation decision guidearchitect.salesforce.com (opens in a new tab)
- Bulk Apex Triggerstrailhead.salesforce.com (opens in a new tab)
- Trigger Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Static and Instance Methods and Variablesdeveloper.salesforce.com (opens in a new tab)
- Sharing Keywordsdeveloper.salesforce.com (opens in a new tab)
- Why is Trigger.newMap unavailable in a before insert trigger?
- When should field updates on the triggering records go in a before trigger?
- Why does the one-trigger-per-object practice exist?
- How can a trigger remember across its own re-invocations in one transaction that work is done?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 21 lessons of the Platform Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (16 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams