Salesforce code snippets
Copy-paste-ready patterns for Apex, SOQL, LWC, the Salesforce CLI, and Flow. Each snippet ships with caveats and links back to the relevant dictionary entries.
- Apex
Safe SOQL with bind variables in Apex
Build dynamic SOQL safely with Database.queryWithBinds. Avoids SOQL injection without resorting to String.escapeSingleQuotes everywhere.
- LWC
LWC: load a record with @wire(getRecord)
Reactive single-record fetch in Lightning Web Components. Cached by the Lightning Data Service so multiple components share one network call.
- SOQL
SOQL: localised picklist values with toLabel()
Returns the translated label for a picklist instead of the raw API value. Useful when the running user has a non-English language set.
- CLI
sf project deploy: deploy only changed files
Use git to diff against main and pass the changed files to `sf project deploy start`. Cuts deploy times from minutes to seconds in large repos.
- Flow
Flow: structured error handler with retry
A reusable Fault path pattern that logs errors to a custom Error_Log__c object, then optionally retries the failed step.