Main Menu
Solutions
- Our Difference
Insights
See AlsoTrailhead Challenges- Get in Touch
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.
- Gettingan Id for your job
- Chaining jobs
- 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.
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:
- In a single transaction, up to 50 jobs canaddedin theSystem.enqueueJob.
- 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_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
Conditional Rendering in LWC
Salesforce Lightning Web Components allows the capability to conditionally render components or elements. If a specific condition is met, the elements or components can be
Save and Resume Forms/Application in OmniScript
Enabling ‘Save and Resume’ functionality, the OmniScript can be paused and resumed without loss of filled data. ‘Save and Resume’ functionality allows the users to
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