Use when writing or reviewing Apex backend code — naming, lightweight layering (service, selector, domain), bulkification, governor limits, sharing keywords, and error handling. Apex triggers and their handlers belong to salesforce-triggers; Apex unit tests to salesforce-apex-testing; access-model design to salesforce-security-model.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Salesforce Apex?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/missing-bits-salesforce-apex)
---
name: salesforce-apex
description: Use when writing or reviewing Apex backend code — naming, lightweight layering (service, selector, domain), bulkification, governor limits, sharing keywords, and error handling. Apex triggers and their handlers belong to salesforce-triggers; Apex unit tests to salesforce-apex-testing; access-model design to salesforce-security-model.
---
# Salesforce Apex
Standards for Apex classes: naming, layering, bulk safety, governor
limits, sharing, and error handling. Triggers and their handlers — one
trigger per object, the trigger body, the project's trigger framework —
belong to `salesforce-triggers`. Cite rules in review
findings as `(standard: salesforce-apex, rule: <id>)`; each rule names
its source — the Apex Developer Guide, Salesforce Well-Architected, a
PMD Apex rule name, or `this standard` (a recorded house decision).
Apex unit tests are out of scope here — see `salesforce-apex-testing`.
Access-model design (org-wide defaults, roles, sharing rules) is out of
scope here — see `salesforce-security-model`; this skill covers only
the `with sharing` / `without sharing` / `inherited sharing` keyword
mechanics.
## Naming
| Element | Convention | Example |
|---|---|---|
| Class | PascalCase, noun | `OrderService` |
| Interface | PascalCase, often an adjective | `Queueable` |
| Method | camelCase, verb-first | `activateFulfillment` |
| Variable / parameter | camelCase | `activatedOrders` |
| Constant (`static final`) | `UPPER_SNAKE_CASE` | `MAX_BATCH_SIZE` |
| Selector | `<Object>Selector`, methods `select*By*` | `AccountSelector.selectByIds(Set<Id>)` |
| Custom exception | `<Domain>Exception` | `OrderProcessingException` |
(id: `apex-naming`; severity: minor; source: Apex Developer Guide; PMD
`ClassNamingConventions`, `MethodNamingConventions`,
`FieldNamingConventions`, `FormalParameterNamingConventions`,
`LocalVariableNamingConventions`; the selector/exception patterns are
this standard)
## Lightweight layers
**No fflib / Apex Enterprise Patterns dependency.** The layering below is
a naming-and-structure convention, not a library.
| Layer | Owns | Never does |
|---|---|---|
| Trigger and handler | Dispatch: hand the trigger context to the layers below — the shape, one trigger per object and the project's framework are `salesforce-triggers`'s | SOQL, DML, business logic in the handler; the trigger body is `trigger-body-delegates`'s to grade |
| Domain (`<Object>Domain`, optional) | Per-record rules on the in-memory set: validation, defaulting, state-transition checks | SOQL, DML, callouts |
| Service (`<Object>Service`) | Orchestration: the business transaction, DML, callouts, chaining async work | Ad-hoc SOQL (asks the selector) |
| Selector (`<Object>Selector`) | ALL SOQL for its object — query methods only | DML, business logic |
- The Domain layer holds an object's own business rules (e.g. "an Order
can only move from Draft to Activated, never backward"), kept
independent of Handler/Service so those rules stay testable without
full orchestration.
- The four framework-independent units for one object — domain,
selector, service, exception — are in
[reference/order-layers.md](reference/order-layers.md); the trigger
and handler that call them are in each `salesforce-triggers`
framework document.
(id: `apex-layering`; severity: important; source: Salesforce
Well-Architected; Apex Enterprise Patterns / fflib cited as
inspiration only, never a requirement — this standard)
## Bulkification
- **No SOQL or DML inside a loop** — collect the working set (`Set<Id>`,
`List<SObject>`), query or write once outside the loop.
- Every trigger-path method assumes it may run against up to 200 records
in one invocation — never code "the single-record case" and add bulk
handling later.
- Before/after pairs and a governor-limit-aware chunking pattern are in
[reference/bulkification.md](reference/bulkification.md).
(id: `apex-bulkification`; severity: important; source: Apex Developer
Guide — "Bulk Apex Triggers"; PMD `OperationWithLimitsInLoop` — the
current rule; older PMD versions expose the same finding as the
now-deprecated `AvoidSoqlInLoops` / `AvoidDmlStatementsInLoops`)
Sub-rules:
- SOQL/DML in a loop reachable from a trigger path (id:
`apex-bulkification.loop-on-trigger-path`; severity: critical; kind:
defect)
## Governor limits
Design to these per-transaction budgets (Apex Developer Guide —
"Execution Governors and Limits"):
| Resource | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries issued | 100 | 200 |
| Rows returned by SOQL | 50,000 | 50,000 |
| DML statements | 150 | 150 |
| Rows processed by DML | 10,000 | 10,000 |
| CPU time | 10,000 ms | 60,000 ms |
| Heap size | 6 MB | 12 MB |
- Use the `Limits` class (`Limits.getQueries()` /
`Limits.getLimitQueries()`, `Limits.getDmlStatements()` /
`Limits.getLimitDmlStatements()`, `Limits.getCpuTime()`,
`Limits.getHeapSize()`, and their `getLimit*` counterparts) to check
remaining budget before a step that might approach it (e.g. choosing
sync processing vs. handing a chunk to a `Queueable`).
- A budget-aware pattern is the third pair in
[reference/bulkification.md](reference/bulkification.md).
(id: `apex-governor-limits`; severity: important; source: Apex
Developer Guide — "Execution Governors and Limits")
## Sharing in code
Every class explicitly declares its sharing keyword — never leave it
implicit:
- **`with sharing`** — the default for user-facing classes: service and
handler layers.
- **`inherited sharing`** — the default for reusable, callable-from-
anywhere layers (selectors, domain classes) that should run in
whatever context called them rather than fix one.
- **`without sharing`** — only with a one-line comment stating why (e.g.
a system-context integration process that must see across the org).
An undeclared or unjustified `without sharing` is a review finding.
This section covers keyword mechanics only. **Access-model design** —
what the org-wide defaults, roles, and sharing rules actually grant —
belongs to `salesforce-security-model`.
(id: `apex-sharing`; severity: important; source: Apex Developer Guide
— "Using the `with sharing` or `without sharing` Keywords"; PMD
`ApexSharingViolations`)
Sub-rules:
- a class doing SOQL/DML without a sharing declaration (id:
`apex-sharing.undeclared-with-dml`; severity: critical; kind:
hardening)
## Error handling
- **Custom exceptions per domain area** — `<Domain>Exception` (e.g.
`OrderProcessingException`, `IntegrationException`), extending
`Exception`. Catch specific exceptions before generic ones; never
catch bare `Exception` and discard it. Catching or throwing a
generic `Exception` instead of the domain exception is a deviation
from this.
- **No empty or silently-swallowed catch blocks.** A catch either
recovers, rethrows (optionally wrapped in a domain exception), or logs
and rethrows — it never just returns or does nothing.
- **Logging is a central abstraction, not `System.debug`.** `System.debug`
is dev-only — not durable, not queryable in production, and lost on
rollback. Route errors (especially integration/callout failures)
through one logging abstraction with levels (error/warn/info) that
persists integration errors past a rollback (e.g. via a platform
event, which commits independently of the triggering transaction). No
library is mandated — Nebula Logger is a reasonable example, not a
requirement.
(id: `apex-error-handling`; severity: important; source: PMD
`EmptyCatchBlock`, `DebugsShouldUseLoggingLevel`; the
central-logger-abstraction and `System.debug`-is-dev-only stances are
this standard)
Sub-rules:
- the caught exception is neither handled nor rethrown — a catch whose
only content is `System.debug` counts as swallowed (id:
`apex-error-handling.swallowed-catch`; severity: critical; kind:
hardening)
- the catch does handle or rethrow, but reports only via
`System.debug` (id: `apex-error-handling.debug-only-reporting`;
severity: minor)