How to use Queueable interface in Apex

Stay away from the limits in Salesforce by implementing the Queueable interface in our Apex classes


Andres Canavesi
Aug 03, 2022
By using queueable, you will stay away of limits, specially CPU limit.
featured image
Async processing is an efficient way to run our algorithms. This mostly involves queues to control how many executions are being processed at a time. Different languages and platforms implement async processing in different ways but the idea is the same. One of the ways (yes, there are more than one) in Salesforce to execute some algorithms that require heavy processing, is the interface Queueable. Another example is the annotation @future but we will put our focus on implementing the interface Queueable in Apex classes. One of the big challenges for most of the async processing approaches is the absence of order on the execution. This is kind of "Do it when possible".
In this example we are just inserting a new account. You may think that's not a heavy operation but what if your org is full of Flows (Process Builder) or triggers that are being executed every time an account is inserted? CPU limit errors will appear soon unless you stay away of the main thread execution.
By using queueable, you will stay away of limits, specially CPU limit.
      
public class MyQueueable implements Queueable{
    
    private final String myAttribute;
    
    public MyQueueable(String myAttribute){
        this.myAttribute = myAttribute;
    }

    public void execute(QueueableContext context) {
	System.debug('executing with: '+myAttribute);
       
        // do some heavy work      
        Account a = new Account(Name=myAttribute);
        insert a;

        // enqueue another job if you wish

    }
    
    public static void enqueueJob(){
        ID jobID = System.enqueueJob(new MyQueueable('my test param'));
        System.debug('job id: '+jobID);
    }
}
      
    

How to enqueue our job

      
ID jobID = System.enqueueJob(new MyQueueable('my test param'));
 
    
Or you can create a method to use it as a shortcut
      
MyQueueable.enqueueJob();
 
    

How to monitor Apex Jobs

You can monitor Apex Jobs from Setup -> Environments -> Jobs -> Apex Jobs
Apex Jobs monitoring
Also you can query your job in case you have to something we them from your code or you just like monitor this way.
      
SELECT Status,NumberOfErrors, ExtendedStatus FROM AsyncApexJob ORDER BY CreatedDate DESC
 
    

How to write unit tests for Apex Jobs

      
@isTest
public class MyQueueableTest {

    @isTest
    static void myTest() {
        String param = 'tes param '+Math.random();
        // startTest/stopTest block to force async processes to run in the test.
        Test.startTest();        
        System.enqueueJob(new MyQueueable(param));
        Test.stopTest();
        
        // Validate that the job has run by verifying that the record was created.
        Account acct = [SELECT Name FROM Account WHERE Name = :param LIMIT 1];
        System.assertEquals(param, acct.Name);
    }
}
      
    
Salesforce docs for Queueable