$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 Class

Share

Write Apex classes with triggers, batch jobs, and best practices

Works with OpenClaude

You 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/*.cls files — use API version 58.0 or later for current features
  • Check sfdx force:org:display to confirm you're connected to the correct Salesforce org
  • Review existing trigger handlers in your codebase to match naming conventions (e.g., AccountTriggerHandler)

Steps

  1. Create a trigger handler class that separates trigger logic from business logic — name it [Object]TriggerHandler and implement the before/after pattern with isBefore, isAfter, isInsert, isUpdate, isDelete context variables
  2. Implement one handler method per operation (e.g., handleBeforeInsert(), handleAfterUpdate()) to keep concerns separated and testable
  3. Add trigger context checks at the start of each handler to prevent recursive updates using a static boolean flag like triggerDisabled
  4. Use Database.query() with proper field selection instead of SELECT * to optimize SOQL limits and only query related records in batch loops
  5. For batch jobs, implement Database.Batchable<SObject> with start(), execute(), and finish() methods; set batch size to 200 for most use cases to balance governor limits
  6. Add @future annotations only for asynchronous callouts or when you need to bypass mixed DML restrictions — avoid unnecessary async calls
  7. Use List.addAll() and Database.insert(records, false) with allOrNone=false to insert partial batches and log failures with Database.SaveResult
  8. Write comprehensive test classes with @isTest annotation, 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

Quick Info

CategorySalesforce
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
salesforceapexdevelopment

Install command:

curl -o ~/.claude/skills/sf-apex-class.md https://clskills.in/skills/salesforce/sf-apex-class.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.