Apex & triggers
Synchronous and asynchronous Apex, governor limits, design patterns, best practices, trigger frameworks and testing, with production-quality code.
Start here: Apex & triggers
Plain-language orientation before the detail.
What this track covers
Apex is Salesforce's server-side programming language: strongly typed, Java-like, and running inside the platform's multitenant runtime. Architects aren't expected to write every line, but they are expected to decide when code is the right tool, how it's structured, and how it behaves at volume. That is what interviewers test.
This track covers:
- Synchronous Apex: transactions, order of execution, governor limits.
- Design and best practices: bulkification, security, error handling, layered design patterns.
- Asynchronous Apex: future, Queueable (with Finalizers,
AsyncOptionsand cursors), Batch and Scheduled Apex, and when to use each. - Triggers and testing: trigger frameworks and tests that prove behaviour.
An analogy: a shared kitchen in a busy food hall
| Food hall | Apex on Salesforce |
|---|---|
| Many restaurants share one kitchen | Many customers (tenants) share the same platform |
| Each order gets a time slot, a set of burners and a fridge shelf | Each transaction gets governor limits: CPU time, queries, DML, heap |
| A cook who fetches one ingredient per trip runs out of time | Code that queries inside loops runs out of limits: bulkify |
| Slow dishes are prepared in the back kitchen and delivered later | Asynchronous Apex moves heavy or slow work out of the user's transaction |
| House rules on who may use which knives | Security: sharing, object and field permissions, user mode |
| One head chef per station decides the order of work | Trigger frameworks: one trigger per object, ordered handlers |
If you remember one thing: everything in Apex is designed around the transaction and its limits.
Same idea, explained to four audiences
| Audience | Say it like this |
|---|---|
| Business sponsor | "We use code only where configuration can't do the job reliably. Where we do, it's structured so changes are safe and fast." |
| Admin | "Flows handle the simple, changing rules. Apex handles complex logic, volume and integrations, and is exposed to Flow as actions you can reuse." |
| Developer | "Bulk-safe services behind thin entry points, one trigger per object, user-mode data access, async for callouts and volume, and tests that assert behaviour." |
| CTA review board | "Code is a liability we accept deliberately. The design bounds it with layering, a trigger framework, async patterns with error recovery, and governance for performance and security." |
When code is the right tool (and when it isn't)
Choose Apex when logic is complex or spans many objects; volume or performance matters; you need callouts with retries; you need precise unit tests; or the same operation is called from many entry points (Flow, LWC, APIs, batch).
Prefer configuration when the rule is simple and changes often, admins own it, and a before-save flow or validation rule can do it.
How to study this track
- Learn the order of execution and the limits table until you can draw and recite them.
- Practise the set-and-map pattern until it's automatic.
- Be able to compare future, Queueable, Batch, Scheduled and Cursors in one table.
- Prepare your trigger framework description and one async integration story with retries.
Misconceptions interviewers like to test
- "
with sharingmakes the code secure." It enforces record sharing only. Object and field permissions need user mode orstripInaccessible. - "Future methods are the fastest async option." All async work is queued; Queueable gives more control and is recommended for new code.
- "75% coverage means the code is tested." Coverage measures lines run, not behaviour proven.
Concept map
How the building blocks fit together. Select a concept to open it.
Synchronous Apex1
Design and best practices2
Asynchronous Apex5
Triggers and testing2
Concepts
Grouped by theme.
Design and best practices
The habits that make Apex survive production: set-based logic, user-mode data access, no hardcoded IDs, deliberate error handling, and configuration in custom metadata.
Separation of concerns (service, domain, selector), unit of work, strategy and factory for variation, dependency injection for tests, and the per-transaction cache.
Asynchronous Apex
Why work goes async, what each async option is good at, and a decision table you can defend in an interview.
The original async option: static void methods with primitive parameters, used for callouts and mixed-DML fixes. Still supported, rarely the best choice for new code.
The default async tool: objects as input, job IDs, chaining, transaction Finalizers for failure handling, delays, duplicate signatures and stack-depth control.
Chunked processing of large data sets: start, execute and finish, scope size, Database.Stateful, the flex queue, error handling and testing.
Running jobs on a timetable with Schedulable and cron expressions, the scheduled-job limit, and why the scheduled class should only start other work.
Triggers and testing
One trigger per object, logic in handler classes, bypass switches, recursion control, and metadata-driven frameworks that order actions and let Flow and Apex coexist.
Writing tests that prove behaviour: test data factories, @TestSetup, bulk and permission tests, testing async code, callout mocks and the Stub API.