You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Queueable Interface


By: Radha

How to define a queueable apex? 

public class QueueableInterfaceExample_AC implements Queueable { 

    public void execute (QueueableContext context) { 

       // perform some logics     

   }  

}

The Queueable interface is same as the future method and both are queued for execution. The queueable interface supports some additional benefits given below.

  1. Getting an Id for your job
  1. Chaining jobs
  1. Using non-primitive types

You can call the queueable class using the enqueue job method.

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

 

Sample code for Queueable Interface:  

public class UpdateLeadDetails_AC implements Queueable { 

    List<Account> leadList = new List<Lead>; 

    public UpdateLeadDetails_AC(List<Lead> leadRecords) { 

        this.leadList = leadRecords; 

    } 

    public void execute(QueueableContext context) { 

        for (Lead leadRec : leadList) { 

                leadRec.Status = 'NON–RESPONSIVE'; 

                leadRec.No_Interest_Status__c = 'Non-responsive'; 

                leadList.add(leadRec); 

        } 

        if(leadList.size() > 0){ 

                 update leadList; 

        } 

    } 

}

 

To add this UpdateLeadDetails_AC  class as a job on the queue, execute the following code:

// Get the list of leads created from last 20 days

List<Lead> leadList = [SELECT Id, Status, No_Interest_Status__c,createddate  FROM Lead WHERE isConverted = false AND isDeleted = false AND status = 'INQUIRED' AND createddate = LAST_N_DAYS:20]; 

// Instantiate a new instance of the Queueable class

UpdateLeadDetails_AC  updateJob = new UpdateLeadDetails_AC (leadList);

// Enqueue the job for processing

ID jobID = System.enqueueJob(updateJob);  

You can use the variable job ID to monitor progress– either through the Apex Jobs page or programmatically by querying AsyncApexJob:

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

Instead of querying, you can use the Apex Jobs to view the status, total batches, job type and completion date.

 

Chaining Jobs: 

One of the best features of queueable apex is job chaining.  You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful when you need to do some sequential processing.

public class FirstJob_AC implements Queueable { 

    public void execute(QueueableContext context) { 

        // Your processing logic here        

  

        // Chain this job to next job by submitting the next job 

        System.enqueueJob(new SecondJob_AC()); 

    } 

}

Previously Salesforce did not support to make a chaining webservice callout, but you can achieve this using queueable interface. Now, we can achieve through apex class, which allows thewebservice callout using chained jobs.

public class FirstJob_AC implements Queueable, Database.AllowCallouts { 

    public void execute(QueueableContext context) { 

        // Your processing logic here        

        // Chain this job to next job by submitting the next job 

        System.enqueueJob(new SecondJob_AC()); 

    } 

} 

On the Apex test class, you can’t test chaining jobs using queueable interface. If you try to chain your jobs, its throws an error. To avoid those errors, you should call the Test.isRunningTest() method before the chaining.

Contrast between @future and Queueable: 

  • You can pass Array of objects to Queueable interface, but in future method it is not supported.
  • You can chain the jobs in the Queueable only.
  • The future method supports certain SObject types only.
  • Future methods cannot be monitored, but queueable apex can be monitored using the job id which is returned by System.enqueueJob()
  • In execution cycle, you cannot call from one future method to another future method. Its achieved inqueueable class by using the Chaining Jobs.

Queueable Interface Limitations: 

  1. In a single transaction, up to 50 jobs can added in the System.enqueueJob.
  1. The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.

Reference: 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm