1. Introduction

  • Briefly explain why asynchronous processing is essential in Salesforce.
  • Mention governor limits and the need for background operations.
  • Set the stage: “In this post, we’ll explore three powerful tools—Batch Apex, Queueable Apex, and Future methods—and when to use each.”

⚙️ 2. Understanding the Tools

🔄 Batch Apex

  • Purpose: Handle large data volumes (up to 50 million records).
  • Key Features:
    • Implements Database.Batchable
    • Methods: start, execute, finish
    • Resets governor limits per batch
  • Use Cases: Data cleansing, mass updates, scheduled jobs
  • Code Snippet:
apex
global class BatchDemo implements Database.Batchable<sObject> {
  global Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator([SELECT Id FROM Account]);
  }
  global void execute(Database.BatchableContext bc, List<Account> scope) {
    for(Account acc : scope) {
      acc.Name += ' - Updated';
    }
    update scope;
  }
  global void finish(Database.BatchableContext bc) {
    // Send notification or log
  }
}

🧵 Queueable Apex

  • Purpose: Flexible job chaining and better control than Future methods
  • Key Features:
    • Implements Queueable
    • Supports job chaining
    • Can pass complex objects
  • Use Cases: Chained jobs, long-running logic, background processing
  • Code Snippet:
apex
public class QueueableDemo implements Queueable {
  public void execute(QueueableContext context) {
    // Your logic here
    System.enqueueJob(new AnotherQueueableJob());
  }
}

Future Methods

  • Purpose: Simple async tasks like callouts or logging
  • Key Features:
    • Annotated with @future
    • Must be static and return void
    • Accepts only primitive parameters
  • Use Cases: Callouts after DML, logging, lightweight async ops
  • Code Snippet:
apex
public class FutureDemo {
  @future(callout=true)
  public static void makeCallout(String endpoint) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('GET');
    HttpResponse res = new Http().send(req);
  }
}

🧪 3. Feature Comparison Table

Feature Batch Apex Queueable Apex Future Method
Record Volume Up to 50M Moderate Low
Chaining
Parameter Flexibility Limited High Primitive only
Monitoring Apex Jobs UI Apex Jobs UI Limited
Use Case Heavy data ops Chained logic Lightweight tasks

🧭 4. Best Practices

  • Avoid calling Future methods from another Future or Batch.
  • Use Queueable for chaining and passing complex data.
  • Schedule Batch jobs for recurring tasks.
  • Monitor jobs via Setup → Apex Jobs.

🧠 5. Real-World Scenario

Create a fictional use case like:

“Imagine a bank wants to update customer records, send notifications, and log the changes. Batch Apex handles the updates, Queueable sends notifications, and Future logs the audit trail.”

📝 6. Conclusion

  • Summarize when to use each method.
  • Encourage readers to experiment and monitor performance.
  • Invite comments or questions for deeper discussion.
Scroll to Top