Intermediate 20 min read

What Is a Trigger?

By the end of this lesson, you'll be able to:

  • Explain what a trigger is and when it runs
  • Recognize a trigger's basic syntax shape
  • Distinguish a trigger from a regular Apex method call

Prerequisites: Module 23: "Project: Inventory System"

Code that runs itself

Every method written so far in this course has needed something to call it explicitly — library.checkOutBook(...), inventory.receiveShipment(...). A trigger is different: it's Apex code tied directly to an object, and Salesforce runs it automatically the moment a matching DML operation happens, with no explicit call anywhere.

The basic shape

trigger AccountTrigger on Account (before insert) {
    System.debug('An Account is about to be inserted.');
}

trigger AccountTrigger on Account names the trigger and the object it's attached to; (before insert) names when it runs — this trigger fires automatically every single time any code, anywhere, runs insert on an Account.

A real-world analogy: a motion-sensor light

A motion-sensor light doesn't need anyone to flip a switch — it reacts automatically the moment it detects movement. A trigger works the same way: it doesn't wait to be called, it reacts automatically the moment its specific event (like Account being inserted) actually happens, anywhere in the org.

Exercise

As a comment, explain the key difference between calling a method like library.checkOutBook(...) and a trigger running.

Show hint

Think about who initiates each one.

APEX

What Is a Trigger? Quiz

1. What makes a trigger different from a regular Apex method?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A trigger is Apex code that runs automatically whenever a record is about to be (or has just been) inserted, updated, deleted, or undeleted — no one has to call it directly.