Write your first trigger, and know why it is a trigger
Ninety minutes, one org, one working automation you keep.
Swarnil Singhai · Namaste Salesforce01 / 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 fundamentals02 / 25
Sixty seconds. If it runs longer you are selling, not teaching.
Ninety minutes
What we are covering
01What a trigger actually isAnd what it is not — 10 min
02The order of executionThe diagram that explains most bugs — 15 min
03BulkificationWhy your loop breaks at 201 records — 15 min
04Hands on: build oneIn your own dev org — 25 min
05Tests, and why 75% is a floorNot a target — 15 min
06Where to go nextThe rest of the course — 10 min
Agenda03 / 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 fundamentals04 / 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 0105 / 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
01Never 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.
02Trigger.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.
03One 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 0106 / 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
1
2
3
4
5
6
Set<Id> ids =newSet<Id>();for(Case c :Trigger.new) ids.add(c.AccountId);// one query, outside the loopMap<Id,Account> byId =newMap<Id,Account>([SELECT Id, Tier__c FROM Account WHERE Id IN:ids]);
Module 03 · bulkification07 / 25
Read line 5 out loud. "One query" is the whole module.
The whole thing, 9 lines
1
2
3
4
5
6
triggerCaseTriggeronCase(before insert,before update){// The trigger holds no logic. It routes. That is the entire job.if(Trigger.isBefore){CaseRouter.route(Trigger.new);}}
// 200 records, because 1 record proves nothing.insertTestData.cases(200);
Module 0308 / 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 0109 / 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 everything100
SOQL queries per synchronous transaction. Every design decision in this module exists because of this one number.
Apex Developer Guide · Execution Governors
Module 0310 / 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 0311 / 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 · loadRecord loadedFrom the DB, or initialised for an insert.
02 · beforeBefore triggersYou are here. No DML needed to change the record.
03 · rulesValidation rulesWhich is why a before trigger can fix data a rule would reject.
04 · afterAfter triggersThe record has an Id. Related records go here.
Module 0212 / 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 example13 / 25
Keep this rail identical on every slide in the module. It is the anchor.
Setup → Object Manager → Case → Triggers
Look at the New button, top right14 / 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 minutes15 / 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.
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.
Check17 / 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 0518 / 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
Week 2
Async: Queueable and Batch
When one transaction is not enough room.
Week 3
Testing properly
Test data factories, assertions, and why 75% is a floor.
Week 4
Integration
Callouts, named credentials, and failing safely.
Course plan19 / 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
// declarativeRecord-triggered flow
Before-save updates are fast and need no code. The default answer for field updates.
// codeApex trigger
For logic a flow cannot express, or that has to be unit tested and reviewed.
// asyncQueueable
When the work does not have to finish before the user's save returns.
Module 0120 / 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 0321 / 25
Optional slide. Cut it first if you are running long.
If you remember one thing
Recap22 / 25
Slow down. This is the sentence you want quoted back to you next week.
Ninety minutes later
What we covered
01What a trigger actually is
02The order of execution
03Bulkification
04You built one
05Tests, and why 75% is a floorNext session, same time.
06Where to go next
Recap23 / 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.
Resources24 / 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.