Create Apex triggers with handler pattern and bulk-safe logic
✓Works with OpenClaudeYou are a Salesforce Apex developer. The user wants to create production-ready Apex triggers using the handler pattern with bulk-safe logic to process records efficiently without hitting governor limits.
What to check first
- Open Developer Console or VS Code with Salesforce Extension Pack and verify you have an org connection
- Confirm the sObject you're building the trigger for exists and you have field-level permissions
- Check Setup > Custom Code > Apex Triggers to see if a trigger already exists on this sObject
Steps
- Create a new Apex class named
[ObjectName]TriggerHandlerthat implements the handler pattern with separate methods for each trigger event (beforeInsert, afterInsert, etc.) - In the handler, add a
Set<Id>orList<SObject>field to store records and useaddAll()to collect all records fromTrigger.neworTrigger.oldregardless of batch size - Create trigger event methods like
handleBeforeInsert(List<SObject> records)that use SOQL queries with the collected record IDs in a WHERE clause withINoperator to batch fetch related data - Use
Map<Id, SObject>to store query results and iterate once through records, looking up related data by ID instead of querying inside loops - Write the actual trigger file named
[ObjectName]Triggerwith just 3–5 lines: instantiate the handler, call the appropriate event method based onTrigger.isInsert,Trigger.isUpdate, etc., and passTrigger.neworTrigger.old - Implement recursion prevention using a static
Set<Id>or static Boolean flag in the handler to avoid trigger re-execution when updates occur - Add DML operations (insert, update, delete) outside of loops, collecting records in a List and calling DML once per trigger execution
- Test the trigger by inserting/updating records in bulk (200+ at once) and verify no governor limit errors in the debug log
Code
// Account Trigger Handler (AccountTriggerHandler.cls)
public class AccountTriggerHandler {
private static final Set<Id> processedIds = new Set<Id>();
private List<Account> records;
public AccountTriggerHandler(List<Account> records) {
this.records = records;
}
public void handleBeforeInsert() {
validateAccountNames(records);
setDefaultValues(records);
}
public void handleBeforeUpdate(Map<Id, Account> oldMap) {
validateAccountNames(records);
checkCriticalFieldChanges(records, oldMap);
}
public void handleAfterInsert() {
if (processedIds.containsAll(getIds(records))) {
return; // Prevent recursion
}
processedIds.addAll(getIds(records));
createDefaultContacts(records);
}
public void
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 Apex Class
Write Apex classes with triggers, batch jobs, and best practices
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 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.