Queueable Interface (2024)

Queueable Interface (1)

Main Menu

  • Technical

Pavithra Gajendran

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 methodandboth are queued for execution. The queueable interface supports some additional benefits given below.

  1. Gettingan 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 thisUpdateLeadDetails_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 variablejob IDto monitor progress–either through the Apex Jobs page or programmatically by queryingAsyncApexJob:

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

Instead of querying, you can use theApex Jobsto view the status, total batches, job type and completion date.

Queueable Interface (2)

Chaining Jobs:

One of the best features ofqueueable apexis job chaining. You can chain one job to another job by starting a second job from a running job. Chaining jobsisusefulwhen youneed 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()); } }

PreviouslySalesforce did not support to make a chainingwebservicecallout, but you can achieve this using queueable interface. Now,we can achieve through apex class, which allows thewebservicecallout 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,itsthrowsan error. To avoid those errors, you should call theTest.isRunningTest() method before the chaining.

Contrastbetween@future and Queueable:

  • You can pass Array of objects toQueueable interface, but infuturemethodit isnot supported.
  • You can chain the jobs in the Queueable only.
  • The future method supports certainSObjecttypes only.
  • Future methods cannot be monitored,butqueueable apex can be monitored using the job id which is returned bySystem.enqueueJob()
  • In execution cycle, you cannot call from one future method to another future method. Its achievedinqueueable classby using the Chaining Jobs.

Queueable Interface Limitations:

  1. In a single transaction, up to 50 jobs canaddedin theSystem.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.

See what makes us unique

Queueable Interface (2024)
Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6323

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.