Skip to main content

What is Queueable Apex in Salesforce?

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. en-queue Job method returns the job id; so, we can easily monitor its progress, either through the Salesforce training 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. Systematization   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 get Content AsPdf() method is not supported in trigger, therefore, here salesforce online training india 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 salesforce online training india, we can monitor the progress of these jobs using the job id.

Comments

Post a Comment

Popular posts from this blog

Why Should Your Nonprofit Use Salesforce Nonprofit Success Pack?

  Salesforce training   is, without a doubt, one of the world’s most advanced CRM solutions. The Salesforce software helps organizations track all of their customer information and interactions in one place. The software features lead management, marketing automation, sales data, partner management, and more. Salesforce created the Nonprofit Success Pack specifically for nonprofits, with the intention of allowing nonprofits to focus on their impact instead of spending a lot of their time collecting and crunching data. Over 30,000 nonprofits around the world use Salesforce – from small human services organizations working in a single community to large multinational NGOs and foundations. The Nonprofit Success Pack features a flexible, open data architecture and pre-built constituent and donor management components. These are the average benefits reported by their customers in their annual “Voice of the Customer” Survey: 89% of Salesforce.org customers say  Salesforce online training  ha