Automate with triggers: Apex practice drills
Before and after triggers, handler classes, bulk patterns, recursion guards and roll-ups.
10 drills · Intermediate · Free to read, run with Apex Path ($19/month after a 7-day trial)
- Write your first before-insert trigger (Beginner, 10 min)
Loop over Trigger.new in a before-insert trigger and give blank Contact descriptions a default, with no DML. - Create follow-up Tasks after Contact insert (Intermediate, 15 min)
Build one related Task per new Contact in an after-insert handler and insert them all with one DML statement. - React to a Contact email change (Intermediate, 15 min)
Compare each Contact with its saved version in Trigger.oldMap and mark only records whose Email really changed. - Route Case trigger contexts (Intermediate, 15 min)
Add the update event to a Case trigger and use Trigger.operationType to send inserts and updates to the right handler method. - Mark parent Accounts in bulk (Intermediate, 18 min)
Collect Account Ids from new Contacts in a Set and mark every parent Account with at most one query and one update. - Move Opportunity logic to a handler (Intermediate, 15 min)
Move the logic out of a busy Opportunity trigger into beforeInsert and beforeUpdate handler methods. - Require a Contact email domain (Intermediate, 18 min)
Use a field-level addError in a trigger handler to reject Contact emails outside @example.com, including look-alike domains. - Stop recursive Account updates (Advanced, 20 min)
Stamp each updated Account once from an after-update trigger, and use a static Set of Ids to stop the update from running the trigger again and again. - Block deleting Accounts with open deals (Advanced, 20 min)
Write a before delete trigger that uses Trigger.oldMap and one query to stop Accounts with open Opportunities from being deleted. - Keep a Contact count on Account (Advanced, 25 min)
Write a Contact trigger that counts the Contacts on each Account again after every insert, update, delete and undelete, with one aggregate query and one update.