• Home
  • Blog
  • Queueable Apex Class with Examples in Salesforce
Queueable Apex Class with Examples in Salesforce

Queueable Apex Class with Examples in Salesforce

1. What is Queueable Apex?

Queueable is one of the four Asynchronous Apex Methods used by a Salesforce Consultant in USA for submiting jobs for Async Processing it’s Similar to future Methods.

2. Future vs Queueable

–> Future Support only Primitive Data Types vs Queueable Support Primitive and non-Primitive Data Types. –> We can’t monitor the jobs in Future vs Queueable monitor job depends on job Id. –> We can’t call one future to another Future or batch apex. In the Queueable we can call one Queueable to another. –> Future method does not return job id vs queueable returns Job id. –> In the Future method You need to specify annotation to identify the Future method in Queueable you do not need to specify any specific annotation for identification.

3. Limitation of Queueable

  1. In the Queueable we can only one job can schedule at a time.
  2. Queueable can’t handle millions of Records at one Job.

4. Benifits of Queueable

1. Queueable support non-Primitive data types like Sobject Ex- Account, Contact, Or Custom Object as well. 2. If we want to chain one job to another job at that time, we can use Queueable. 3. If we want a job id, we will get using queueable, and using a job Id you can easily track the job.

5. Syntax Queueable

Implementation queueable Interface- public class SampleClass implements Queueable { public void execute (QueueableContext Qx){ // Here we Writing Code } } queueable Interface:-  queueable Interface is the enhanced way of running asynchronous apex code comparing to future async.

6. Steps Implementation

Following the below example, we are creating an Account record and also, we tracking jobs Basically, two ways to track job Id: –
  1. UI Level
  2. SOQL Level
Below the Example we are creating a class QueueableDemo implements Queueable Interface public class QueueableDemo implements Queueable{ public void execute(QueueableContext Qx){ system.debug(‘Here The execute method of QueueableDemo class’); Account account = new Account(); account.Name = ‘Test Account’; insert account; system.debug(‘Account record inserted Here—->’+account.Name); } }

6.1 Tracking jobs Id from Apex jobs steps

Click on the Gear Setting Button select Set-Up-> Click the quick find box and Search Apex Jobs -> Here you will get a list of job IDs and with help of a particular job Id you can easily track the job Queueable

6.2 Tracking Job Id Help Of SOQL

Click below Query Editor tab -> Enter Here Soql -> Click to Execute You will get all details here, as You Enter in the query Query Example- SELECT Id, Status, JobItemsProcessed, NumberOfErrors FROM AsyncApexJob where Id =                                ‘7075g00005dtaQg’.–>ExQueueable

Chaining Job in Queueable

Chaining a job is kind of chaining the job from one to another. Basically, chaining a job means running job after Some other processing is completed first by another job. In the below example, we changing jobs and also show the logs Similarly here three queueable class QueueableDemo, QueueableDemoFirst, and QueueableDemoSecond are implemented by Queueable.

1. QueueableDemo

public class QueueableDemo implements Queueable{ public void execute(QueueableContext Qx){ system.debug(‘Here The execute method of QueueableDemo class’); Account account = new Account(); account.Name = ‘Test Account’; insert account; system.debug(‘Account record inserted Here—->’+account.Name); Id jobId = system.enqueueJob(new QueueableDemoFirst()); system.debug(‘QueueableDemoFirst job id is—->’+jobId); } } Execute the below code in an anonymous window–> Id jobId = system.enqueueJob(new QueueableDemo());    system.debug(‘QueueableDemo job id is–>’+jobId); 

2. QueueableDemoFirst

public class QueueableDemoFirst implements Queueable{ public void execute(QueueableContext Qx){ system.debug(‘Here The execute method of QueueableDemoFirst class’); Contact contact = new Contact(); contact.LastName = ‘Test Contact’; insert contact; system.debug(‘Contact Record Inserted Here —->’+contact.LastName); Id jobId = system.enqueueJob(new QueueableDemoSecond()); system.debug(‘QueueableDemoSecond job id is—->’+jobId); } }

3. QueueableDemoSecond

public class QueueableDemoSecond implements Queueable{ public void execute(QueueableContext Qx){ system.debug(‘Here The execute method of QueueableDemoSecond class’); StudentDetail__c studentDetails = new StudentDetail__c(); studentDetails.StudentAddress__c = ‘Abc Street’; insert studentDetails; system.debug(‘StudentDetail Record Inserted Here—->’+     studentDetails.StudentAddress__c); } } Queueable Queueable Queueable
Conclusion:- Queueable apex is the Asynchronously running in the background. All job is going to the queueForm and Jobs are running when the system resource is available. Also, we can monitor the job help of job id.