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);

}

}

How to Call :

apex

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

With Callout Example :

apex

public class CalloutFutureExample {

@future(callout=true)

public static void callExternalApi(String url) {

Http h = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint(url);

req.setMethod(‘GET’);

HttpResponse res = h.send(req);

System.debug(‘Response: ‘ + res.getBody());

}

}

Code Explanation

public class FutureExample : {

A class named FutureExample. It holds our future method.

@future

: Marks the method to run asynchronously (in the background).

public static voidsendWelcomeEmail (String email):

A method that takes an email (as a string). It must be static and return void.

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

This line prints the email to the debug log. Simulates sending an email.

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 :

A more flexible way to run async code. You can :

• Chain jobs

• Pass complex data types (like Lists, Maps, SObjects)

• Monitor job status in Apex Jobs

Syntax :

apex

public class MyQueueableJob implements Queueable {

public void execute(QueueableContext context) {

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

}

}

How to Call :

apex

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

Chaining Queueable Jobs:

apex

public class FirstJob implements Queueable {

public void execute(QueueableContext context) {

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

System.enqueueJob(new SecondJob());

}

}

public class SecondJob implements Queueable {

public void execute(QueueableContext context) {

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

}

}

Passing Data :

apex

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 = acc.Name + ‘ – Updated’;

}

update accounts;

}

}

apex

 

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

System.enqueueJob(new ProcessAccounts(accs));

Monitor :

Go to Setup > Apex Jobs to track the job’s progress and outcome.

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().