Queueable Apex in Salesforce

Introduction


Queueable Apex is a powerful asynchronous execution framework in Salesforce that allows developers to perform complex and long-running operations. It builds upon the functionality of @future methods and provides additional features like chaining jobs and accessing non-primitive data types.

Key Features of Queueable Apex

  • Enables job chaining.
  • Allows passing of non-primitive data types.
  • Provides better monitoring and debugging capabilities.

Syntax
Queueable Apex classes must implement the Queueable interface.

public class QueueableExample implements Queueable {
    public void execute(QueueableContext context) {
        // Business logic here
        List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];
        for (Account acc : accounts) {
            acc.Name = acc.Name + ' - Updated';
        }
        update accounts;
    }
}

How to Enqueue a Job
QueueableExample job = new QueueableExample();
Id JobId = System.enqueueJob(job);

Chaining Jobs
public class ChainingQueueableExample implements Queueable {
    public void execute(QueueableContext context) {
        // First job logic
        System.enqueueJob(new AnotherQueueableJob());
    }
}

Monitoring Jobs

You can monitor Queueable jobs from the Apex Jobs page in Salesforce Setup.

Limitation for Asynchronous

From / WhatCall @future methodSystem.enqueueJob()Database.executeBatch()System.schedule()
Anonymous Apex5050100100
@AuraEnabled method5050100100
@future method010100
Queueable execute()501100100
Schedulable execute()5050100100
Batch start()010100
Batch execute()010100
Batch finish()010100
Platform Event trigger5050100100
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *