Triggers: Apex practice drills
Before and after triggers, handler classes, bulk patterns, recursion guards and roll-ups.
Intermediate · Check and Run with Apex Path ($19 a month).
- Write your first before-insert trigger Beginner · 10 min
A before insert trigger sees new records in Trigger.new before Salesforce saves them.
- Create follow-up Tasks after Contact insert Intermediate · 15 min
Use after insert when related records need the new records' Ids.
- React to a Contact email change Intermediate · 15 min
An update trigger sees the new record in Trigger.new and the saved one in Trigger.oldMap, keyed by Id.
- Route Case trigger contexts Intermediate · 15 min
One trigger can handle several events.
- Mark parent Accounts in bulk Intermediate · 18 min
A bulk-safe trigger handler collects parent Ids first and runs one query and one update for the whole batch.
- Move Opportunity logic to a handler Intermediate · 15 min
A trigger should pick the event; a handler class should do the work.
- Require a Contact email domain Intermediate · 18 min
A before-trigger handler can reject a single record with a field-level addError.
- Stop recursive Account updates Advanced · 20 min
A static variable keeps its value for the whole transaction, so a static Set<Id> can remember which records a trigger already handled.
- Block deleting Accounts with open deals Advanced · 20 min
A delete trigger has no Trigger.new; the records are in Trigger.old and keyed by Id in Trigger.oldMap.
- Keep a Contact count on Account Advanced · 25 min
A rollup counts child rows on a parent by running one aggregate query, not by adding or subtracting 1 per event.
- Create a child record only when a field changes Intermediate · 18 min
In an after update trigger, compare the new value to the old value from Trigger.oldMap before you create a child record.