ApexSensei

All drills

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)

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Move Opportunity logic to a handler (Intermediate, 15 min)
    Move the logic out of a busy Opportunity trigger into beforeInsert and beforeUpdate handler methods.
  7. 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.
  8. 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.
  9. 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.
  10. 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.

Start practicing free