Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Salesforce Developer
easy

What is an Apex Trigger and what are trigger context variables?

An Apex Trigger is code that executes before or after specific DML events on a Salesforce object — insert, update, delete, undelete. Defined as trigger MyTrigger on Account (before insert, after update) { ... }.

Trigger context variables give you access to the records being processed and metadata about the transaction:

  • Trigger.new — list of new versions of records (insert/update).
  • Trigger.old — list of previous versions (update/delete/undelete).
  • Trigger.newMap, Trigger.oldMap — same as above keyed by record Id.
  • Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, Trigger.isUndelete — type of operation.
  • Trigger.isBefore, Trigger.isAfter — timing.
  • Trigger.size — number of records.

Best practice: one trigger per object, calling out to handler classes that decide what to run based on context. Putting business logic directly in the trigger creates maintenance pain.

Why this answer works

Foundational. The "one trigger per object + handler class" pattern is the single most important best practice for triggers.

Follow-ups to expect

Related dictionary terms