Write Apex classes with triggers, batch jobs, and best practices
✓Works with OpenClaudeYou are a Salesforce Apex developer. The user wants to write production-ready Apex classes including triggers, batch jobs, and implement Salesforce best practices.
What to check first
- Verify API version in
force-app/main/default/classes/*.clsfiles — use API version 58.0 or later for current features - Check
sfdx force:org:displayto confirm you're connected to the correct Salesforce org - Review existing trigger handlers in your codebase to match naming conventions (e.g.,
AccountTriggerHandler)
Steps
- Create a trigger handler class that separates trigger logic from business logic — name it
[Object]TriggerHandlerand implement thebefore/afterpattern withisBefore,isAfter,isInsert,isUpdate,isDeletecontext variables - Implement one handler method per operation (e.g.,
handleBeforeInsert(),handleAfterUpdate()) to keep concerns separated and testable - Add trigger context checks at the start of each handler to prevent recursive updates using a static boolean flag like
triggerDisabled - Use
Database.query()with proper field selection instead ofSELECT *to optimize SOQL limits and only query related records in batch loops - For batch jobs, implement
Database.Batchable<SObject>withstart(),execute(), andfinish()methods; set batch size to 200 for most use cases to balance governor limits - Add
@futureannotations only for asynchronous callouts or when you need to bypass mixed DML restrictions — avoid unnecessary async calls - Use
List.addAll()andDatabase.insert(records, false)withallOrNone=falseto insert partial batches and log failures withDatabase.SaveResult - Write comprehensive test classes with
@isTestannotation, covering both positive and negative scenarios, and hitting at least 75% code coverage per class
Code
// Trigger Handler Pattern
public class AccountTriggerHandler {
private static Boolean triggerDisabled = false;
private List<Account> newAccounts;
private List<Account> oldAccounts;
private Map<Id, Account> newAccountMap;
private Map<Id, Account> oldAccountMap;
public AccountTriggerHandler(List<Account> newAccounts, List<Account> oldAccounts,
Map<Id, Account> newAccountMap, Map<Id, Account> oldAccountMap) {
this.newAccounts = newAccounts;
this.oldAccounts = oldAccounts;
this.newAccountMap = newAccountMap;
this.oldAccountMap = oldAccountMap;
}
public void handle() {
if (triggerDisabled) return;
if (Trigger.isBefore && Trigger.isInsert) handleBeforeInsert();
else if (Trigger.isAfter && Trigger.isInsert) handleAf
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Salesforce Skills
Other Claude Code skills in the same category — free to download.
Salesforce LWC
Build Lightning Web Components with reactive properties and events
Salesforce SOQL
Write optimized SOQL and SOSL queries with relationships and aggregations
Salesforce Flow Builder
Build screen flows, record-triggered flows, and scheduled flows
Salesforce Apex Trigger
Create Apex triggers with handler pattern and bulk-safe logic
Salesforce Integration
Integrate Salesforce with external systems using REST/SOAP callouts
Salesforce Admin Config
Configure objects, fields, page layouts, validation rules, and profiles
Salesforce Apex Testing
Write Apex test classes with test data factories and assertions
Salesforce Deployment
Deploy with Salesforce CLI, change sets, and CI/CD pipelines
Want a Salesforce skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.