Queueable Apex In Salesforce

by Rijwan Mohmmed
0 comment
queueable-apex-in-salesforce-techdice

Hello folks, today we will discuss the Queueable Apex In Salesforce. By using the Queueable interface we can control asynchronous Apex processes. This interface enables you to add jobs to the queue and monitor them. Using the interface is an enhanced way of running your asynchronous Apex code compared to future methods. Queueable Apex class is an advanced version of the future methods.

Also, check this: Device Form Factor in LWC Salesforce

Key Highlights :

  1. Getting an ID for your job: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the new job. This ID corresponds to the ID of the AsyncApexJob record. Use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
  2. Using non-primitive types: Your queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.
  3. Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if your process depends on another process to have run first.
  4. You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
  5. When chaining jobs with System.enqueueJob, you can add only one job from an executing job. Only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job isn’t supported.

Code :

Queueable v/s Future :

Because queueable methods are functionally equivalent to future methods, most of the time you’ll probably want to use queueable instead of future methods. However, this doesn’t necessarily mean you should go back and refactor all your future methods right now.

Another reason to use future methods instead of queueable is when your functionality is sometimes executed synchronously, and sometimes asynchronously. It’s much easier to refactor a method in this manner than converting to a queueable class. This is handy when you discover that part of your existing code needs to be moved to async execution.

Queueable Apex Class Syntax :

public class AsyncExecutionExample implements Queueable {
    public void execute(QueueableContext context) {
             //your logic
    }
}

ContactQueueable.cls :

public class ContactQueueable implements Queueable {
    public List<Contact> contactList ; 
    public AccountQueueableExample(List<Contact> contactList){
        this.contactList = contactList ;  
    }

    public void execute(QueueableContext context) {
        for(Contact con : contactList){
            // Update the Contact Last name 
            con.LastName = con.LastName + 'techdicer';
        }
        update contactList;
    }
}

Call the Queuqable class :

List<Contact> conList = [Select Id, Lastname from Contact];
ID jobID = System.enqueueJob(new ContactQueueable(conList));
System.debug('jobID ==> ' + jobID);

ContactQueueableTest.cls :

@isTest
public class ContactQueueableTest {
    static testmethod void unitTest() {
        Contact cont = new Contact();
        cont.lastName = 'Test';
        cont.email = 'techdicer@techdicer.com';
        insert cont;
        List<Contact> conList = [Select Id, Lastname from Contact];
        //startTest/stopTest block to force async processes to run in the test.
        Test.startTest();        
        System.enqueueJob(new ContactQueueable(conList));
        Test.stopTest();
    }
}

Reference :

  1. Queueable Apex
  2. Control Processes with Queueable Apex

You may also like

Leave a Comment