Mytutorialrack

queueable apex salesforce

Queueable Apex Salesforce: Queueable Apex in Salesforce is an improved version of future methods, combining their simplicity with the power of Batch Apex. It offers a class structure that the platform handles for you, without needing start and finish methods. Additionally, it allows you to use more than just simple arguments. You call it with the System.enqueueJob() method, which gives you a job ID to monitor. To use Queueable Apex, you need to implement the Queueable interface.

Advantage of using queueable Apex

Queueable jobs are like future methods because they both run tasks in the background. However, Queueable jobs have these benefits over future methods:

  • Getting an ID for your job: When we start our job using the System.enqueueJob method, it gives us a job ID. This ID is the same as the AsyncApexJob record ID. We can use this ID to find and track the job’s progress. We can do this in the Salesforce user interface on the Apex Jobs page or by querying the AsyncApexJob record programmatically.
  • Using non-primitive types: Your queueable class can have member variables that are not basic data types, like sObjects or custom Apex types. You can use these objects when the job runs.
  • Chaining jobs: You can link one job to another by starting a second job from a running job. This is helpful if you need to do something that depends on another task being completed first.

Queueable Versus Future

Queueable methods work like future methods, so you’ll usually want to use queueable methods instead. However, you don’t need to change all your existing future methods right away. If a future method is hitting a governor limit or needs higher limits, you might be able to increase the limits with the Future Methods with Higher Limits pilot program.

Another reason to use future methods instead of queueable methods is when your code sometimes runs right away and sometimes runs in the background. It’s easier to change a future method to run in both ways than to change it to a queueable class. This is useful if you find out that part of your existing code needs to run asynchronously.

Queueable Apex salesforce Syntax

We need to create an Apex class with the Queueable interface, which has one method called execute. If you want to make callouts from the Queueable Apex, you also need to implement the Database.AllowsCallouts interface.

public class ExampleClassName implements Queueable { 
    public void execute(QueueableContext context) {
        // awesome code here
    }
}

Execute Queueable Apex

Run the Queueable Apex using the System.enqueueJob method, which will give you a job ID. You can use this job ID to track the job’s progress in the Apex Jobs section or by using the AsyncApexJob object.

ID jobID = System.enqueueJob(new SomeClassName());

Queueable Apex Example

In this example we will append Mytutorialrack at the end of account name

public class AccountQueueableExample implements Queueable {
    public List<Account> accList ; 
    public AccountQueueableExample(List<Account> accList){
        this.accList = accList ;  
    }
    public void execute(QueueableContext context) {
        for(Account acc :accList){
            // Update the Account Name 
            acc.Name = acc.Name + 'Mytutorialrack';
        }
        update accList;
    }
}

Run the job from the execute anonymous with below code.

List<Account> accList = [Select Id , Name from Account ];
ID jobID = System.enqueueJob(new AccountQueueableExample(accList));
System.debug('jobID'+jobID);

Queueable Job chaining

One great feature of Queueable Apex is job chaining. If you ever need to run jobs one after another, Queueable Apex can simplify this process for you. To chain one job to another, you submit the second job from the execute() method of your queueable class. You can only add one job from a running job, so each parent job can have only one child job. For instance, if you have another class named SecondJob that implements the Queueable interface, you can add it to the queue within the execute() method like this:

Queueable Apex limits

Queueable Apex is a useful new tool, but there are a few things to be cautious about:

  •  Each queued job execution counts once towards the shared limit for asynchronous Apex method executions.
  • You can add up to 50 jobs to the queue using System.enqueueJob in one go. When chaining jobs, you can only add one job from an executing job using System.enqueueJob. This means each parent queueable job can only have one child job. Starting multiple child jobs from the same queueable job is not allowed.
  • There’s no strict limit on how deep you can chain jobs. You can link one job to another and keep chaining new child jobs as needed. However, in Developer Edition and Trial orgs, you can only chain jobs up to a maximum depth of 5. This means you can chain jobs four times, with a total of 5 jobs in the chain, including the first parent queueable job.

Share:

Recent Posts