← Back to list

Salesforce Asynchronous Techniques —  Future Methods, Queueable Apex, Batch Apex and Scheduled Apex

In Salesforce, there are several mechanisms for executing operations asynchronously, including Future Methods, Queueable Apex, Batch Apex…

SHIVAM VERMA · 2024-11-25 16:04 · 1 claps · 3.8 min read
#salesforce-development #salesforce-asynchronous
Open on Medium ↗
Wiki topics: CRM · Email & CRM

Salesforce Asynchronous Techniques — Future Methods, Queueable Apex, Batch Apex and Scheduled Apex

In Salesforce, there are several mechanisms for executing operations asynchronously, including Future Methods, Queueable Apex, Batch Apex, and Scheduled Apex. Each of these options has different use cases, benefits, and limitations, and it’s important to understand the differences to choose the right one for your needs.

1. Future Methods (@future)

Use Case: Simple, lightweight asynchronous operations.

  • Purpose: Allows you to run operations asynchronously in the background, outside of the current transaction. Useful for processes like callouts to external systems or updating records without affecting the user experience.
  • Execution Context: Runs asynchronously in the background and doesn’t block the transaction.
  • Limitations:
  • Single Callout Per Invocation: A future method can only execute one HTTP callout per invocation.
  • Limited to Simple Logic: Can only accept primitive data types (such as String, Integer, Id) as parameters, not complex objects.
  • Maximum 50 Calls per Transaction: A single transaction can invoke up to 50 future methods.
  • No Chaining: Future methods cannot invoke other future methods (chaining).
  • Example Use Cases: Sending an email, making a single HTTP callout, or updating records asynchronously.

Syntax Example:

@future(callout=true)
public static void myFutureMethod(Id recordId) {
    // Asynchronous logic here (e.g., callouts, processing)
}

2. Queueable Apex

Use Case: Complex asynchronous processes, with better flexibility and control.

  • Purpose: Allows for more complex, flexible, and chainable asynchronous processing. Suitable for scenarios where you need to enqueue jobs and perform background processing that can be more complicated than future methods.
  • Execution Context: Runs asynchronously outside the current transaction.
  • Key Features:
  • Flexible Parameters: Can pass complex data types (such as custom objects) as parameters, unlike future methods.
  • Chaining Jobs: You can chain multiple queueable jobs. A queueable job can enqueue another queueable job for further processing.
  • Control Over Job Execution: You can have fine-grained control over the job execution and can implement custom logic inside the execute() method.
  • No Callout Limitation: Supports HTTP callouts (with the Database.AllowsCallouts interface) and doesn’t have the one-callout limit that future methods do.
  • Limit: Can enqueue up to 50 jobs per transaction.

Example Use Cases:

  • Complex business logic that requires multiple steps (e.g., data aggregation, processing, or updates across multiple records).
  • Chaining jobs for multi-step workflows.
  • HTTP callouts with more control over data processing.

Syntax Example:

public class MyQueueableClass implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        // Asynchronous logic here (e.g., callouts, processing)
    }
}

// Enqueueing a job from elsewhere in the code:
System.enqueueJob(new MyQueueableClass());

3. Batch Apex

Use Case: Handling large data volumes and breaking them into manageable chunks.

  • Purpose: Allows you to process large sets of records asynchronously in manageable chunks, with the ability to handle millions of records in a scalable way.
  • Execution Context: Executes in the background asynchronously.
  • Key Features:
  • Processing Large Data Volumes: Batch Apex is designed for operations that need to process large datasets, breaking them into smaller chunks (called “batches”).
  • Customizable Batch Size: You can define the batch size (e.g., 200 records per batch).
  • Multiple Methods: It involves implementing three methods: start(), execute(), and finish().
  • Limits: A batch job can run up to 5,000 records per batch and can process a large number of batches. You can process up to 50,000 records across all batches in a single run.
  • Supports Callouts: Batch Apex supports HTTP callouts with the Database.AllowsCallouts interface.

Example Use Cases:

  • Data migration, cleaning, or mass updates.
  • Processing records that span across multiple pages of a report.
  • Integrating with external systems where you need to process large datasets and handle callouts.

Syntax Example:

global class MyBatchClass implements Database.Batchable<SObject>, Database.AllowsCallouts {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Id FROM Account WHERE Active__c = true]);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope) {
        // Process each batch of records
    }

    global void finish(Database.BatchableContext BC) {
        // Final processing after all batches are completed
    }
}

// Running the batch job
ID batchJobId = Database.executeBatch(new MyBatchClass(), 200);

4. Scheduled Apex

Use Case: Running jobs at specific times or on a recurring schedule.

  • Purpose: Allows you to schedule an Apex class to run at specific intervals (daily, weekly, etc.) or at a specific time. Useful for tasks that need to be automated on a regular basis.
  • Execution Context: Executes asynchronously according to the schedule you set.
  • Key Features:
  • Time-based Execution: You can specify the exact time and frequency for a job to run (e.g., every day at midnight or once a month).
  • Limited Frequency: You can only schedule up to 100 jobs per org at any given time.
  • Callouts Supported: Scheduled Apex can include callouts and process data asynchronously.
  • Job Queuing: Scheduled jobs are queued and executed automatically at the scheduled time.

Example Use Cases:

  • Sending reports or notifications on a regular schedule.
  • Batch data processing that needs to be done during off-hours.
  • Syncing data with external systems on a recurring basis.

Syntax Example:

global class MyScheduledClass implements Schedulable {
    global void execute(SchedulableContext context) {
        // Logic to run at the scheduled time
    }
}

// Scheduling the job
String cronExp = '0 0 0 * * ?'; // Schedule to run at midnight every day
System.schedule('My Scheduled Job', cronExp, new MyScheduledClass());

Key Differences

When to Use Each:

  • Future Methods: Best for simple, lightweight asynchronous tasks, such as a single HTTP callout or small data updates.
  • Queueable Apex: Use when you need more flexibility, the ability to chain jobs, or pass complex data objects. Ideal for moderately complex tasks and workflows.
  • Batch Apex: Use when you need to process large volumes of records in chunks, especially when dealing with tens of thousands or more records.
  • Scheduled Apex: Use when you need to run operations on a regular schedule, like daily or weekly, or need to process data periodically.

메타데이터
post_id
930aebcf3891
slug
salesforce-asynchronous-techniques-future-methods-queueable-apex-batch-apex-and-scheduled-apex-930aebcf3891
url
https://medium.com/@shivamverma02/salesforce-asynchronous-techniques-future-methods-queueable-apex-batch-apex-and-scheduled-apex-930aebcf3891
canonical_url
https://medium.com/@shivamverma02/salesforce-asynchronous-techniques-future-methods-queueable-apex-batch-apex-and-scheduled-apex-930aebcf3891
author_url
https://medium.com/@shivamverma02
status
ok
fetched_at
2026-08-19 05:11:59