$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
SalesforceintermediateNew

Salesforce Apex Trigger

Share

Create Apex triggers with handler pattern and bulk-safe logic

Works with OpenClaude

You 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

  1. Create a new Apex class named [ObjectName]TriggerHandler that implements the handler pattern with separate methods for each trigger event (beforeInsert, afterInsert, etc.)
  2. In the handler, add a Set<Id> or List<SObject> field to store records and use addAll() to collect all records from Trigger.new or Trigger.old regardless of batch size
  3. Create trigger event methods like handleBeforeInsert(List<SObject> records) that use SOQL queries with the collected record IDs in a WHERE clause with IN operator to batch fetch related data
  4. Use Map<Id, SObject> to store query results and iterate once through records, looking up related data by ID instead of querying inside loops
  5. Write the actual trigger file named [ObjectName]Trigger with just 3–5 lines: instantiate the handler, call the appropriate event method based on Trigger.isInsert, Trigger.isUpdate, etc., and pass Trigger.new or Trigger.old
  6. Implement recursion prevention using a static Set<Id> or static Boolean flag in the handler to avoid trigger re-execution when updates occur
  7. Add DML operations (insert, update, delete) outside of loops, collecting records in a List and calling DML once per trigger execution
  8. 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

Quick Info

CategorySalesforce
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
salesforceapextrigger

Install command:

curl -o ~/.claude/skills/sf-trigger.md https://clskills.in/skills/salesforce/sf-trigger.md

Related Salesforce Skills

Other Claude Code skills in the same category — free to download.

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.