Distinguish Apex before-trigger and after-trigger semantics and choose the correct context for field defaulting versus cross-object writes

domain: developer.salesforce.com · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

In-depth guide

Salesforce Bulk API 2.0 limits — the full failure-mode walkthrough related to developer.salesforce.com, checked against official docs, with linked verified routes.

Steps

  1. Use a before-insert or before-update trigger to set or modify fields on the records in Trigger.new directly — changes here are saved without a separate DML call
  2. Use an after-insert or after-update trigger when you need the system-assigned record Id (unavailable before insert) or when writing to related records via a separate DML statement
  3. Never issue DML on the SObject type of the triggering records inside a before trigger — this causes a self-triggering loop and a runtime error
  4. For validation that should prevent save, throw a custom exception or call record.addError() inside a before trigger; addError() in an after context still rolls back the transaction
  5. When both before and after logic is needed, use a single trigger with both contexts delegating to one handler to avoid ordering ambiguity between two separate trigger files
  6. Document the context choice in comments to make it clear to future maintainers why a particular context was selected

Known gotchas

Related routes

Implement one-trigger-per-object Apex pattern with a trigger handler framework to centralize logic and enable testability
developer.salesforce.com · 6 steps · unrated
Implement static flag-based recursion control in an Apex trigger handler to prevent re-entrant trigger execution
developer.salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans