← back to docs
Apex fundamentals · module 01

Write your first trigger, and know why it is a trigger

Ninety minutes, one org, one working automation you keep.

Swarnil Singhai · Namaste Salesforce 01 / 25

Don't read the title. Ask who has written Apex before — split the room and pitch to the smaller half.

Before we start

Who is teaching this

  • Nine years on the platformAdmin, then developer, then the person who fixes what both shipped.
  • 14,000 learnersNamaste Salesforce — courses, not certifications-in-a-weekend.
  • Everything here is openThe org, the code and these slides ship with the course.
Apex fundamentals 02 / 25

Sixty seconds. If it runs longer you are selling, not teaching.

Ninety minutes

What we are covering

  1. 01What a trigger actually isAnd what it is not — 10 min
  2. 02The order of executionThe diagram that explains most bugs — 15 min
  3. 03BulkificationWhy your loop breaks at 201 records — 15 min
  4. 04Hands on: build oneIn your own dev org — 25 min
  5. 05Tests, and why 75% is a floorNot a target — 15 min
  6. 06Where to go nextThe rest of the course — 10 min
Agenda 03 / 25

Say the break out loud — after item 04. People stop listening when they don't know when they can leave.

Module 01

What a trigger actually is

Ten minutes. One idea, and then we look at real code.

Apex fundamentals 04 / 25

Pause here. Two seconds of silence does more for a transition than any animation.

The whole module in one line

A trigger is not an event handler. It is a batch handler that usually gets a batch of one.

Module 01 05 / 25

Let it sit. Ask: "who has written a trigger that worked in the UI and broke on an import?" — most hands go up.

Consequences

Three things that follow from that

  • 01 Never query inside a loop200 records × one SOQL each is 200 queries against a limit of 100. The org stops you, mid-import, in front of the customer.
  • 02 Trigger.new is a list, alwaysEven when a user saved one record. Code that reads Trigger.new[0] is code that works until someone uploads a CSV.
  • 03 One trigger per objectTwo triggers on Account run in an order the platform does not promise. That is not a style preference; it is a correctness one.
Module 01 06 / 25

Reveal one at a time. Point 03 is the one they will argue with — let them.

The pattern

Collect the ids, query once, map, then act

  • 01Walk the batch, collect the ids
  • 02One SOQL, outside the loop
  • 03Index it into a Map
  • 04Walk the batch again and decide
CaseRouter.cls
Set<Id> ids = new Set<Id>();for (Case c : Trigger.new) ids.add(c.AccountId);// one query, outside the loopMap<Id, Account> byId = new Map<Id, Account>(  [SELECT Id, Tier__c FROM Account WHERE Id IN :ids]);
Module 03 · bulkification 07 / 25

Read line 5 out loud. "One query" is the whole module.

The whole thing, 9 lines
trigger CaseTrigger on Case (before insert, before update) {  // The trigger holds no logic. It routes. That is the entire job.  if (Trigger.isBefore) {    CaseRouter.route(Trigger.new);  }}
Module 03 08 / 25

Click the second tab. The test is the punchline: 200, not 1.

The question you will be asked

Flow or Apex?

Flow

  • No deployment for small changes
  • An admin can maintain it next year
  • Harder to unit test, harder to review

Apex — when it is genuinely logic

  • Real tests, real version control
  • Handles bulk without thinking about it
  • Needs a developer, forever
Module 01 09 / 25

Say the honest version: most orgs pick Apex because the developer was in the room, not because it was right.

The limit that shapes everything 100

SOQL queries per synchronous transaction. Every design decision in this module exists because of this one number.

Apex Developer Guide · Execution Governors
Module 03 10 / 25

Ask what happens at 101. Someone will say "error"; the real answer is "your import stops half-done".

Know these three

The limits you will actually hit

100
SOQL queries, synchronous. Execution Governors
150
DML statements per transaction. Execution Governors
6MB
Heap, synchronous. Execution Governors
Module 03 11 / 25

All three land together — they are one idea, not three. That is what data-fragment-group is for.

Where your code runs

The order of execution, abridged

01 · load Record loaded From the DB, or initialised for an insert.
02 · before Before triggers You are here. No DML needed to change the record.
03 · rules Validation rules Which is why a before trigger can fix data a rule would reject.
04 · after After triggers The record has an Id. Related records go here.
Module 02 12 / 25

The whole module is step 02 vs step 04. Everything else is context.

Working example

Route every case to the right queue, at save time

  • 01A case arrives from email, web or a phone callThree origins, three teams, one object.
  • 02The queue depends on the account's tierWhich lives on a different object — hence the SOQL.
  • 03It has to survive a 200-row importBecause on Monday someone will do exactly that.
Working example 13 / 25

Keep this rail identical on every slide in the module. It is the anchor.

Setup → Object Manager → Case → Triggers
The Triggers section of the Case object in Setup, with the New button highlighted
Look at the New button, top right 14 / 25

Don't narrate the whole screen. Name the one control they need.

Live demo
Switching to the org

Watch it break at 201 records

Developer Console → Execute Anonymous → the naive version, then the bulkified one.

Demo · about 6 minutes 15 / 25

Have the two scripts already open in tabs. Never type live — you will typo, and the room will watch you debug instead of learn.

Hands on
Your turn

Build the router in your own org

  1. 01Create CaseRouter.cls from the snippet
  2. 02Wire it to a before-insert trigger
  3. 03Insert 200 cases from Execute Anonymous
  4. 04Check the debug log for query count
Open the exercise

Time remaining — click to start

Stuck? The finished class is in the course files.

Module 04 · hands on 16 / 25

Walk the room. Do not answer from the front — go to the person.

Quick check
Everyone answer

A user imports 500 cases. How many times does your trigger run?

AOnce — one import, one transaction
BThree times — 200, 200, 100
C500 times — once per record
Show the answer

B. The platform chunks a bulk load into batches of 200, and each batch is its own transaction with its own governor limits. This is why per-transaction limits are the ones that matter, and why "it worked on my one test record" proves nothing.

Check 17 / 25

Count hands for each before revealing. If B is under half, do the demo again.

Definition of done

Before this goes to production

  • One trigger on the object, logic in a handler class
  • No SOQL or DML inside a loop — read it again and check
  • A test that inserts 200 records, not one
  • A test that asserts something — coverage is not a test
  • A rollback plan written down before the deploy, not during it
Module 05 18 / 25

Item 4 is the one that gets skipped. Say the number: 75% coverage with no assertions is 0% tested.

The rest of the course

What we build from here

  1. Week 2
    Async: Queueable and Batch When one transaction is not enough room.
  2. Week 3
    Testing properly Test data factories, assertions, and why 75% is a floor.
  3. Week 4
    Integration Callouts, named credentials, and failing safely.
Course plan 19 / 25

This is also the sales slide. Don't sell it — just say what happens next.

Pick the right tool

Three ways to automate a save

// declarative Record-triggered flow

Before-save updates are fast and need no code. The default answer for field updates.

// code Apex trigger

For logic a flow cannot express, or that has to be unit tested and reviewed.

// async Queueable

When the work does not have to finish before the user's save returns.

Module 01 20 / 25

Ask which one they reach for first. The honest answer is "the one I know", and that is the actual lesson.

Bulkify everything. The platform will hand you two hundred records on a day you are not watching.
Apex Developer Guide · Best Practices
Module 03 21 / 25

Optional slide. Cut it first if you are running long.

If you remember one thing
Recap 22 / 25

Slow down. This is the sentence you want quoted back to you next week.

Ninety minutes later

What we covered

  1. 01What a trigger actually is
  2. 02The order of execution
  3. 03Bulkification
  4. 04You built one
  5. 05Tests, and why 75% is a floorNext session, same time.
  6. 06Where to go next
Recap 23 / 25

Name what is NOT covered too. "We did not touch async" saves you three questions.

Take it with you

Everything from today

  • The finished codegithub.com/imswarnil/apex-fundamentals
  • These slidesnamastesalesforce.com/decks/apex-01
  • The written lessonSame material, at your own pace.
  • Ask a questionThe course discussion — answered within a day.
Resources 24 / 25

Leave this slide up during questions. It is the most useful thing the wall can be doing.

That is module 01

Questions

Next session: tests, and why 75% coverage is a floor rather than a target.

Continue the course
Swarnil Singhai · namastesalesforce.com 25 / 25

Do not end on "any questions?" into silence. Ask the first one yourself.

Apex fundamentals · module 01 01 / 25