Queueable Apex

Queueable Apex is similar to future methods, but provide additional job chaining and allow more complex data types to be used.

Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits:

  1. 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.
  2. Monitoring: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the AsyncApexJob record. You can 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.
  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 you need to do some sequential processing.

Things to Remember

Queueable Apex is a great new tool but there are a few things to watch out for:

  1. The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.
  2. You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
  3. When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job is a no-no.
  4. No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat this process with each new child job to link it to a new child job. However, for Developer Edition and Trial orgs, the maximum stack depth for chained jobs is 5, which means that you can chain jobs four times and the maximum number of jobs in the chain is 5, including the initial parent queueable job.

Example:

https://www.infallibletechie.com/2018/10/how-to-transfer-tasks-back-to-lead.html

Sample Code:

Queueable Apex:

public class AddPrimaryContact implements Queueable {  
  
    Contact con = new Contact();  
    String strState = '';  
  
    public AddPrimaryContact(Contact con, String strState) {  
  
        this.con = con;  
        this.strState = strState;  
  
    }  
  
    public void execute(QueueableContext qc) {  
  
        List < Account > listAccount = [ SELECT Id FROM Account WHERE BillingState =: strState LIMIT 200 ];  
        List < Contact > listContact = new List < Contact >();  
  
        for ( Account acc : listAccount ) {  
  
            Contact c = con.clone();  
            c.AccountId = acc.Id;  
            listContact.add(c);  
  
        }  
  
        insert listContact;  
  
    }  
  
}  

To Call the Queuebale apex:

Account a = new Account( Name = 'Test' );  
insert a;  
Contact c = new Contact( LastName = 'Testing', AccountId = a.Id );  
insert c;  
System.enqueueJob( new AddPrimaryContact( c, 'TN' ) );  

Leave a Reply