Asynchronous Apex
What Is Asynchronous Apex?
Short Description
Asynchronous Apex allows code to run in the background, outside the main transaction, so Salesforce stays fast and responsive.
Simple Explanation
Asynchronous Apex is like sending work to a background worker while users continue their tasks.
When to Use Async Apex
-
Large data processing
-
Callouts to external systems
-
Long-running operations
-
Scheduled or delayed work
Gist (Quick Revision)
Async Apex runs in the background to handle heavy or delayed tasks safely.
1. Future Methods
What Is a Future Method?
A Future method runs asynchronously and is used for simple background tasks.
Key Rules
-
Must be
static -
Must return
void -
Limited parameter types
Real-Life Example
Sending a confirmation email after saving a record.
Code Example
@future
public static void sendNotification(Id accountId) {
System.debug('Notification sent for ' + accountId);
}
When to Use
-
Simple logic
-
One-time async processing
-
No job chaining needed
Gist (Quick Revision)
Future methods are best for simple, fire-and-forget tasks.
2. Queueable Apex
What Is Queueable Apex?
Queueable Apex is more powerful than Future methods and supports job chaining.
Simple Explanation
Queueable is Future method’s smarter replacement.
Real-Life Example
Process records, then notify another system.
Code Example
public class MyQueueableJob implements Queueable {
public void execute(QueueableContext context) {
System.debug('Queueable job running');
}
}
System.enqueueJob(new MyQueueableJob());
Advantages Over Future
-
Supports complex data types
-
Supports chaining
-
Easier testing
Gist (Quick Revision)
Queueable Apex is flexible, powerful, and preferred over Future methods.
3. Batch Apex
What Is Batch Apex?
Batch Apex processes large volumes of records in small chunks.
Simple Explanation
Instead of lifting everything at once, Batch Apex lifts in pieces.
Real-Life Example
Updating millions of records safely.
Batch Apex Structure
public class AccountBatch implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id FROM Account');
}
public void execute(Database.BatchableContext bc, List<Account> scope) {
// process each batch
}
public void finish(Database.BatchableContext bc) {
System.debug('Batch completed');
}
}
How to Run Batch
Database.executeBatch(new AccountBatch(), 200);
Gist (Quick Revision)
Batch Apex is used for very large datasets processed safely in chunks.
4. Scheduled Apex
What Is Scheduled Apex?
Scheduled Apex runs code at a specific time or interval.
Simple Explanation
Like setting an alarm clock for Apex code.
Real-Life Example
Generate monthly reports every 1st day.
Code Example
public class MonthlyJob implements Schedulable {
public void execute(SchedulableContext sc) {
System.debug('Scheduled job running');
}
}
Schedule the Job
System.schedule(
'Monthly Job',
'0 0 0 1 * ?',
new MonthlyJob()
);
Gist (Quick Revision)
Scheduled Apex runs code automatically at defined times.
5. Chaining Jobs
What Is Job Chaining?
Chaining means starting another async job after one finishes.
Why It Matters
-
Break large work into steps
-
Avoid governor limits
-
Control execution flow
Queueable Chaining Example
public class FirstJob implements Queueable {
public void execute(QueueableContext context) {
System.enqueueJob(new SecondJob());
}
}
Real-Life Example
Step 1: Process data
Step 2: Send notification
Step 3: Update status
Gist (Quick Revision)
Chaining allows async jobs to run one after another safely.
