Asynchronous Apex in Salesforce

Asynchronous Apex in Salesforce refers to executing code asynchronously, meaning that the code is executed in the background without blocking the user interface. Asynchronous Apex is typically used for long-running or resource-intensive processes that would otherwise exceed the transaction execution time limits.

Queueable Apex

Take control of your asynchronous Apex processes by using the Queueable interface. This interface enables you to add jobs to the queue and monitor them. Using the interface is an enhanced way of running your asynchronous Apex code compared to using future methods.

Apex Scheduler

Batch Apex

Future Methods

This table lists the asynchronous Apex features and when to use each.

Here'r multiple ways for running your Apex code asynchronously.

















@future Annotation: Methods annotated with @future are queued for execution in a separate transaction. This allows you to perform tasks asynchronously. @future methods can accept parameters and return void, making them suitable for various use cases. However, they have limitations, such as not being able to return results synchronously or accepting certain types of parameters.

global class AsyncClass {
    @future
    public static void myFutureMethod(String param1) {
        // Perform asynchronous processing here
    }

}

Queueable Interface: Implementing the Queueable interface allows you to define custom asynchronous jobs that can be enqueued for execution. Queueable jobs can be chained together to execute sequentially or in parallel. They have advantages over @future methods, such as better error handling, support for complex data types, and monitoring capabilities.

public class MyQueueable implements Queueable {
    public void execute(QueueableContext context) {
        // Perform asynchronous processing here
    }
}

Batch Apex: Batch Apex allows you to process large data sets in smaller, manageable chunks. It's particularly useful for tasks like data cleansing, data migration, or complex calculations on large data volumes. Batch Apex jobs can be scheduled to run at specific times or executed manually.

global class MyBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext context) {
        // Return the query locator for the data to process
    }
    
    global void execute(Database.BatchableContext context, List<sObject> scope) {
        // Process the data in chunks
    }
    
    global void finish(Database.BatchableContext context) {
        // Perform any post-processing tasks
    }
}

Scheduled Apex: Scheduled Apex allows you to schedule Apex classes to run at specific times. This is useful for tasks that need to be performed on a recurring basis, such as sending email notifications, updating records, or generating reports.

global class MyScheduled implements Schedulable {
    global void execute(SchedulableContext context) {
        // Perform scheduled processing here
    }
}

Asynchronous Apex provides flexibility and scalability for handling complex business logic and data processing requirements in Salesforce. However, it's essential to design and implement asynchronous code carefully to ensure efficient resource utilization and compliance with governor limits.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach