Apex Asynchronous Processing (Future, Queueable)

Apex Asynchronous Processing

In Apex, some operations take time (like API calls, data processing, etc.). To avoid slowing down the user experience, Asynchronous Apex lets you run those operations in the background — after the current process finishes.

Why Use Asynchronous Apex ?

• Long-running operations

• Avoid hitting governor limits

• Improve user experience

• Perform callouts to external systems

1. @future Method

Definition :

Runs a method asynchronously in the background after the current process is complete.

Rules :

• Must be static and return void

• with @future

• Parameters must be primitive data types (no SObjects)

• Can perform callouts if declared with @future(callout=true)

Syntax :

apex

public class FutureExample {

@future

public static void sendWelcomeEmail(String email) {

// Simulate sending email

System.debug(‘Sending email to: ‘ + email);

}

}

Code Explanation

public class FutureExample: Declares a public class named FutureExample.
• @future: Tells Salesforce to run the method asynchronously (in the background). 
public static void sendWelcomeEmail(String email): A method that takes an email address as input. It’s static and returns nothing (void), as required by @future. 
• System.debug(…): Logs a message simulating that an email is being sent. 

How to Call This Method:

FutureExample.sendWelcomeEmail(‘test@example.com‘); 

This will queue the email to be “sent” (logged) in the background, without blocking the main thread. 

Practice Tasks :

1. Create a future method that accepts a String name and prints “Hello, [name]” using System.debug().

2. Create a future method with callout=true and fetch any dummy API.

3. Modify your future method to log the date and time when it runs using Datetime.now().

2. Queueable Apex

Definition :

Queueable Apex offers a structured, scalable approach to asynchronous execution. It enhances the functionality of @future methods and facilitates the following: 

Job chaining 

 Passing complex data types (Lists, Maps, SObjects) 

Monitoring execution via Apex Jobs

Syntax :

public class MyQueueableJob implements Queueable { 

    public void execute(QueueableContext context) { 

        System.debug(‘Running queueable job…’); 

    } 

} 

 

How to Execute (Anonymous Window):

1.Open Developer Console (Gear icon → Developer Console)

2.Navigate to Debug > Open Execute Anonymous Window

3.Paste the following code to enqueue your job:

ID jobId = System.enqueueJob(new MyQueueableJob()); 

Check ‘Open Log’, then click “Execute” 

Chaining Queueable Jobs:

Use job chaining to trigger another queueable job once the current one finishes: 

public class FirstJob implements Queueable { 

    public void execute(QueueableContext context) { 

        System.debug(‘First job running…’); 

        System.enqueueJob(new SecondJob()); // Chain second job 

    } 

} 

  

public class SecondJob implements Queueable { 

    public void execute(QueueableContext context) { 

        System.debug(‘Second job running…’); 

    } 

} 

 

Execution (Anonymous Window): 

System.enqueueJob(new FirstJob()); 

Passing Complex Data (Example with List of Accounts): 

public class ProcessAccounts implements Queueable { 

    List<Account> accounts; 

  

    public ProcessAccounts(List<Account> accs) { 

        this.accounts = accs; 

    } 

  

    public void execute(QueueableContext context) { 

        for (Account acc : accounts) { 

            acc.Name += ‘ – Updated’; 

        } 

        update accounts; 

    } 

} 

Execution (Anonymous Window): 

List<Account> accs = [SELECT Id, Name FROM Account LIMIT 5]; 

System.enqueueJob(new ProcessAccounts(accs)); 

Monitoring Jobs: 

Navigate to:

Setup > Apex Jobs

You can view status: Queued, Processing, Completed, or Failed

Click on the Job ID for execution logs or exception traces

Practice Tasks :

1. Create a basic Queueable class that prints a message to the debug log.

2. Write a Queueable job that updates the name of 5 Account records by appending “ – VIP”.

3. Chain two Queueable jobs: First one logs “Job 1 started”, the second logs “Job 2 running”.

4. Pass a list of Leads into a Queueable job and mark their status as “Contacted”.

5. Enqueue your Queueable job and store its Job ID. Print it with System.debug().