Queueable Interface

The Queueable interface is same as the future method and both are queued for execution. You can call the queueable class using the enqueue job method.

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
  2. Chaining jobs
  3. 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.

queueable interface

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.
  2. 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

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.