You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Queueable Apex


By: Venkata

Queueable apex is an asynchronous apex method. It’s similar to the @future method. By using this queueable interface, we can process an Apex that runs for a long time (such as web service call outs and extensive database operations). The job isadded to Apex job queue. System.enqueueJob method returns the job id; so, we can easily monitor its progress, either through the Salesforce UI in the Apex Jobs page, or by querying the record using AsyncApexJob object.

Benefits of Queueable Apex: 

1. Queueable apex supports the Non-primitive type   member variables,  such as SObject or custom apex types.

2. System.enqueueJob   method returns the AsyncApexJob record id. Use this id to find your job and Monitor its progress.

3. You can chain one job with another job.

Limitations:

1. Count against shared asynchronous method call limit.

2. Can add only up to 50 jobs with in single transaction.

3. Only one job does the call out in chaining.

Syntax:

   1.Implement Queueable Interface 

Public class ClassName_AC Implements Queueable { 

//Constructor is optional 

Public ClassName_AC (){ 

} 

global void executes (QueueableContext context) { 

// do stuff 

} 

} 

   2.Callout from Queueable  

Public class ClassName_AC Implements Queueable,Database.AllowsCallouts  { 

global void executes (QueueableContext context) { 

// do stuff 

} 

}

3.Chaining in Queueable 

Public class ClassName1_AC Implements Queueable { 

global void executes (QueueableContext context) { 

// do stuff 

Id jobId =System.enqueueJob(new ClassName2_AC ()); 

} 

} 

Public class ClassName2_AC Implements Queueable { 

global void executes (QueueableContext context) { 

// do stuff 

} 

}

 

Example: 

The following example attaches a PDF File to the Account attachment section whenever a new Account is inserted.  Since getContentAsPdf() method is not supported in trigger, therefore, here we used Queueable Apex that runs asynchronously.

AccountTrigger_AT 

Trigger AccountTrigger_AT on Account (after Insert) { 

    if (Trigger.isInsert && Trigger.isAfter) { 

        AccountTriggerHandler_AC.onAfterInsert(trigger.new, trigger.newMap); 

    } 









AccountTriggerHandler_AC 

Public class AccountTriggerHandler_AC { 

    public static void onAfterInsert(List<Account> newAccountList, Map<Id, Account> newAccountMap) { 

        Set<Id> accountIdSet =new Set<Id> (); 

       Id jobId; 

        for (Account newAcc: newAccountList) { 

                accountIdSet. add (newAcc.Id);                

            

        }         

         if (accountIdSet. Size () > 0) jobId= System.enqueueJob(new AttachPDF_AC (accountIdSet)); 

    }    

} 



AttachPDF_AC (Queueable Interface Class) 

global class AttachPDF_AC implements Queueable, Database.AllowsCallouts { 

      Set<Id> AccountIdSet = new Set<Id> (); 

       

      global AttachPDF_AC (Set<Id> AccountIdSet) { 

        this. AccountIdSet. addAll (AccountIdSet);          

     } 

      

     global void executes (QueueableContext context) { 

        PageReference pdfPage;   

        Attachment attachmentPDF; 

        List<Attachment> attachmentPDFList = new List<Attachment> ();            

        for (Id accId: AccountIdSet) { 

            pdfPage = Page.AttachPDF_VF; 

            pdfPage.getParameters(). put ('Id’, accId); 

            Blob pdf = pdfPage.getContentAsPdf(); 

            attachmentPDF=new Attachment (); 

            attachmentPDF.parentId = accId; 

            attachmentPDF.Name = accId +'.pdf'; 

            attachmentPDF.body = pdf; 

            attachmentPDFList.add(attachmentPDF); 

       } 

       if (attachmentPDFList.Size() > 0) 

             Insert attachmentPDFList;             

    } 

} 

Note: 

System.enquueJob() is return the job id and the following code is used to Monitor the job through programmatically.

AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobId];

Visualforce Page

AttachPDF_VF

<apex: page standardController="Account" renderAs="PDF"> 

     <apex: form > 

        Name: <apex: outputText value="{! Account.Name}"/><br /> 

        Account Number <apex: outputText value="{!Account.AccountNumber}"/><br /> 

         Created Date <apex: outputText value="{!Account.CreatedDate}"/><br /> 

         Created Date <apex: outputText value="{! Account.lastmodifiedDate}"/><br /> 

     </apex: form>      

</apex: page>

 

Conclusion: 

Queueable Apex asynchronously runs in background in its own thread. All jobs are added to a Queue and each job runs when the system resource are available. Also, we can monitor the progress of these jobs using the job id.