Sub topic 1
Why move work out of the synchronous transaction
Prerequisites
Assume you know basic Apex classes, SOQL, DML, governor limits, triggers, and unit tests. A synchronous transaction is the request that runs immediately, such as a trigger execution or controller action.
Why move work out of the synchronous transaction
Synchronous Apex must finish before Salesforce can return a response. It has shared limits for CPU time, database operations, and callouts. A trigger that performs heavy processing can exceed those limits or make users wait.
Asynchronous Apex moves work into a separate transaction. The original transaction can finish sooner, while Salesforce runs the queued work later. The asynchronous job cannot return a result directly to the original request, so design it for eventual completion.
What changes in async: limits and org-wide caps
Asynchronous transactions receive different per-transaction governor limits. For example, the maximum CPU time and some database limits are higher than in synchronous Apex. These limits still apply inside each job.
Async work also competes for org-wide capacity. A large number of queued jobs can wait, and Salesforce limits how many asynchronous executions an org can submit or run. Splitting work into jobs does not remove limits; it changes where and when they apply.
Future methods: simple, fire-and-forget work
A future method is a static void method marked with @future. It accepts only primitive values or collections of primitives, such as a Set<Id>. Use callout=true when it performs an HTTP callout.
public class PartnerSync {
@future(callout=true)
public static void sendAccountNames(Set<Id> accountIds) {
List<Account> accounts = [
SELECT Id, Name FROM Account WHERE Id IN :accountIds
];
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:Partner_API/accounts');
request.setMethod('POST');
request.setBody(JSON.serialize(accounts));
HttpResponse response = new Http().send(request);
System.debug(response.getStatusCode());
}
}Future methods are useful for small, fire-and-forget tasks. They cannot return a value or provide a convenient job object for tracking.
Queueable Apex: future methods with fewer restrictions
Queueable Apex uses a class that implements Queueable. It can hold richer state, including sObjects and collections, and System.enqueueJob returns a job ID. Queueable jobs can also chain another queueable job.
public class AccountReviewJob implements Queueable {
private List<Account> accounts;
public AccountReviewJob(List<Account> accounts) {
this.accounts = accounts;
}
public void execute(QueueableContext context) {
for (Account account : accounts) {
account.Description = 'Reviewed';
}
update accounts;
}
}
Id jobId = System.enqueueJob(new AccountReviewJob(accounts));Choose Queueable when the job needs state, monitoring, or sequential follow-up work. Use a future method when the operation is small and its restrictions are acceptable.
Exercise
Which tool is the better default for an asynchronous operation that must carry a list of Account records and expose a job ID?
Show answer
Queueable Apex, because it can hold sObject state and System.enqueueJob returns an ID.