SalesforceBlue

Feel the rhythm of Salesforce

Apex

Queueable Apex Simplified

Queueable Apex is similar to Future Methods with an extra set of features and capabilities.

Unlike future methods, they can have non-primitive data types such as sObjects and custom Apex Types to be passed.

Let’s see the basic code block for Queueable Apex :

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

In the above code block, in order to declare a class as Queueable, we have to implement the Queueable interface and define the execute method.

Let’s do a queueable call from the synchronous transaction.

public class BlueUtility {
    public static void makeQueueableCall() {
        // Some cool stuff happening

        System.enqueueJob(new QueueableClass());
    }
}

In the above code block, we have invoked the Queueable class by using System.enqueueJob. This method accepts an instance of the Queueable class which is supposed to be called.

If any error occurred and the Apex transaction rolls back, any queueable jobs queued for execution by the transaction are not processed for that transaction.

System.enqueueJob returns a job Id which we can use to monitor the status of the running Queueable job either in UI or in Apex using the below query.

AsyncApexJob jobStatus = [SELECT Id, Status, NumberOfErrors FROM AsyncApexJob WHERE Id = :jobID]

In the above query jobId is the Id returned by the System.enqueueJob method.

The ID of a queueable Apex job isn’t returned in the test context. System.enqueueJob returns null in a running test.

Chaining Queueable Jobs:

You can call one queueable job from a running Queueable job allowing sequential processing in separate asynchronous transactions. This is called as chaining of Jobs.

Consider you want another Queueable job to run when one queueable job finishes. To achieve this you can do chaining of Jobs.

One such scenario can be that you receive a response from a web service in one job and now you wanted to invoke a second Queueable job to call another web service using the response returned from the first one.

Let’s see them in action below:

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

        System.enqueueJob(new AnotherQueueableClass());
    }
}
public class AnotherQueueableClass implements Queueable { 
    public void execute(QueueableContext context) {
        // awesome code here
    }
}

In the above code block, we are chaining Jobs by calling invoking AnotherQueueableClass in the execute method of QueueableClass

You can only chain a single job from a parent’s job. If you try to chain more than one queueable job then Apex will allow you to save the code but on run time you will get an error – “Too many queueable jobs added to the queue”.

You can not chain queueable jobs in an Apex test, doing so results in an error. To avoid any errors, you can check if Apex is running in a test context by calling Test.isRunningTest() before chaining jobs.

Calling External Services From Queable Apex:

Apex allows HTTP and web service callouts from queueable jobs if they implement the Database.AllowsCallouts marker interface. In queueable jobs that implement this interface, callouts are also allowed in chained queueable jobs.

public class QueueableClass implements Queueable, Database.AllowsCallouts { 
    public void execute(QueueableContext context) {
        // awesome code here
    }
}
Queueable Apex Governor Limits Considerations:

You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. In asynchronous transactions (for example, from a batch Apex job), you can add only one job to the queue with System.enqueueJob.

There is no limit enforced to the depth of the chained jobs. However, there are limits on developer and trial org. The maximum number is 5 including the initial parent queueable job.

When chaining jobs with System.enqueueJob, you can add only one job from an executing job. Only one child job can exist for each parent’s queueable job.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

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