Guides selection and implementation of async Apex patterns (@future, Queueable, Batch, Schedulable, Platform Events, chaining) for governor limits, callouts from triggers, bulk processing, and scheduling.
npx claudepluginhub jiten-singh-shahi/salesforce-claude-code --plugin salesforce-claude-codeThis skill is limited to using the following tools:
Implementation guidance for asynchronous Apex. Covers when to use each pattern and how to implement it correctly. Governor limit numbers and hard rules live in the referenced files and `sf-apex-constraints`.
Provides patterns for Salesforce platform development: Lightning Web Components (LWC), Apex triggers/classes, REST/Bulk APIs, Connected Apps, Salesforce DX with scratch orgs and 2GP.
Writes and debugs Apex code, builds Lightning Web Components, optimizes SOQL queries, implements triggers, batch jobs, platform events, and Salesforce integrations. Use for CRM workflows, governor limits, bulk processing, and Salesforce DX CI/CD.
Identifies Salesforce pitfalls like SOQL N+1 queries, governor limit violations, API overuse, and SOQL injection during code reviews, onboarding, and integration audits.
Share bugs, ideas, or general feedback.
Implementation guidance for asynchronous Apex. Covers when to use each pattern and how to implement it correctly. Governor limit numbers and hard rules live in the referenced files and sf-apex-constraints.
Reference: @../_reference/ASYNC_PATTERNS.md
@future, Queueable, Batch, Schedulable, or Platform Events| Requirement | Pattern |
|---|---|
| Simple async with no sObject params | @future |
| Need to pass sObjects or collections | Queueable |
| Need callouts from trigger context | @future(callout=true) |
| Need callouts with complex state | Queueable + Database.AllowsCallouts |
| Processing millions of records | Batch Apex |
| Need state across batches | Batch Apex + Database.Stateful |
| Run on a schedule | Schedulable (wraps Batch or Queueable) |
| Decouple publisher from subscriber | Platform Events |
| Chain jobs with delay | Queueable + AsyncOptions |
The simplest async mechanism. Runs in a separate transaction with its own governor limits.
public class ExternalDataSync {
@future(callout=true)
public static void syncAccountToERP(Id accountId) {
Account acc = [
SELECT Id, Name, BillingCity, AnnualRevenue
FROM Account WHERE Id = :accountId LIMIT 1
];
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_System/accounts');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(new ERPAccountPayload(acc)));
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
logSyncError(accountId, res.getStatusCode(), res.getBody());
}
}
}
@future from another @future throws a runtime exception.More powerful than @future. Supports sObject parameters, chaining, and monitoring via AsyncApexJob.
public class AccountEnrichmentJob implements Queueable {
private final List<Account> accounts;
public AccountEnrichmentJob(List<Account> accounts) {
this.accounts = accounts;
}
public void execute(QueueableContext context) {
List<Account> toUpdate = new List<Account>();
for (Account acc : accounts) {
if (acc.AnnualRevenue != null && acc.NumberOfEmployees != null
&& acc.NumberOfEmployees > 0) {
toUpdate.add(new Account(
Id = acc.Id,
Revenue_Per_Employee__c = acc.AnnualRevenue / acc.NumberOfEmployees
));
}
}
if (!toUpdate.isEmpty()) update toUpdate;
}
}
// Enqueue
System.enqueueJob(new AccountEnrichmentJob(accounts));
Implement Database.AllowsCallouts alongside Queueable.
public class ContactDataEnrichmentJob implements Queueable, Database.AllowsCallouts {
private final Set<Id> contactIds;
public ContactDataEnrichmentJob(Set<Id> contactIds) {
this.contactIds = contactIds;
}
public void execute(QueueableContext context) {
// Query, callout, update pattern
}
}
Use chaining to process large data sets across multiple transactions. Use WHERE clauses to naturally shrink the result set instead of OFFSET (which has a 2,000-row hard limit).
public class DataMigrationChainJob implements Queueable {
private static final Integer BATCH_SIZE = 200;
public void execute(QueueableContext context) {
List<Legacy_Record__c> batch = [
SELECT Id, Legacy_Field__c
FROM Legacy_Record__c
WHERE Migrated__c = false
ORDER BY CreatedDate
LIMIT :BATCH_SIZE
];
if (batch.isEmpty()) return; // Migration complete
processBatch(batch);
// Chain next job — WHERE Migrated__c = false naturally shrinks each iteration
System.enqueueJob(new DataMigrationChainJob());
}
}
// Delay execution by 5 minutes
System.AsyncOptions opts = new System.AsyncOptions();
opts.minimumQueueableDelayInMinutes = 5;
System.enqueueJob(new MyQueueableJob(data), opts);
// Duplicate prevention with a unique key
System.AsyncOptions opts2 = new System.AsyncOptions();
opts2.duplicateSignature = 'account-sync-' + accountId;
System.enqueueJob(new AccountSyncJob(accountId), opts2);
For processing large data volumes (millions of records) that exceed single-transaction limits.
public class AccountAnnualReviewBatch
implements Database.Batchable<SObject>, Database.Stateful {
private Integer processedCount = 0;
private List<String> errors = new List<String>();
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([
SELECT Id, Name, AnnualRevenue, Last_Annual_Review__c, OwnerId
FROM Account
WHERE Type = 'Customer'
AND (Last_Annual_Review__c = null
OR Last_Annual_Review__c < LAST_N_DAYS:365)
]);
}
public void execute(Database.BatchableContext bc, List<Account> scope) {
// Process scope — each execute() is its own transaction
// Default scope = 200 records
}
public void finish(Database.BatchableContext bc) {
// Cleanup and notifications
}
}
// Execute (default scope of 200)
Database.executeBatch(new AccountAnnualReviewBatch());
// Custom scope (smaller for complex processing or callouts)
Database.executeBatch(new AccountAnnualReviewBatch(), 50);
Implement Database.AllowsCallouts and set scope = 1 when each callout is per-record (each execute() is limited to 100 callouts).
public class SingleRecordCalloutBatch
implements Database.Batchable<SObject>, Database.AllowsCallouts {
// scope = 1 in executeBatch call
}
Database.executeBatch(new SingleRecordCalloutBatch(), 1);
Runs Apex on a schedule. Best practice: schedulable should only coordinate, not do heavy work.
public class WeeklyReportScheduler implements Schedulable {
public void execute(SchedulableContext sc) {
Database.executeBatch(new WeeklyReportBatch(), 200);
}
}
// Schedule
String cronExp = '0 0 6 ? * MON'; // Every Monday at 6:00 AM
System.schedule('Weekly Report - Monday 6AM', cronExp, new WeeklyReportScheduler());
0 0 2 * * ? — Daily at 2:00 AM
0 0 9 ? * MON-FRI — Weekdays at 9:00 AM
0 0 0 1 * ? * — First day of every month at midnight
0 30 8 ? * SAT — Every Saturday at 8:30 AM
Decouple publishers from subscribers. Subscribers run in their own transaction.
List<Order_Status_Change__e> events = new List<Order_Status_Change__e>();
for (Order__c order : orders) {
events.add(new Order_Status_Change__e(
Order_Id__c = order.Id,
New_Status__c = newStatus,
Changed_By__c = UserInfo.getUserId(),
Timestamp__c = Datetime.now()
));
}
List<Database.SaveResult> results = EventBus.publish(events);
By default, high-volume platform events use "publish after commit" behavior. To publish immediately regardless of transaction outcome, configure the event's Publish Behavior to "Publish Immediately" in Setup.
trigger OrderStatusChangeTrigger on Order_Status_Change__e (after insert) {
for (Order_Status_Change__e event : Trigger.new) {
// Process event — runs in its own transaction
}
}
trigger HighVolumeEventTrigger on Analytics_Event__e (after insert) {
// Set resume checkpoint for retry-after-failure
EventBus.TriggerContext.currentContext().setResumeCheckpoint(
Trigger.new[Trigger.new.size() - 1].ReplayId
);
}
Test.startTest() / Test.stopTest() forces @future, Queueable, and Batch jobs to execute synchronously. Platform events are also delivered synchronously within the test boundary.
@isTest
static void testBatchUpdatesReviewDate() {
// Insert test data
Test.startTest();
Database.executeBatch(new AccountAnnualReviewBatch(), 200);
Test.stopTest(); // All batch methods run synchronously
// Assert results
}
sf-review-agent, sf-apex-agent — For interactive guidancesf-apex-constraints — Governs limits, bulkification rules, and naming conventions for all Apex code including async