queueable apex in salesforcev

Queueable Apex in Salesforce - Concept to Implementations

INTRODUCTION

Queueable Apex is an apex code that runs Asynchronously like a future method.

Here the term Asynchronous means running in the background .

Queueable apex and future method both run sdfs asynchronously but queueable apex provides these additional benefits.

1. Getting Id of your Job:- ID JobId =system.enqueueJob(qe);
2. Chaining Job:- In Queueable Apex we can chain one job to another.
3. Using Non-primitive data types:-

dont miss out iconDon't forget to check: How to Write a Batch Apex in Salesforce

Queueable is a interface whereas future is a method.

  1. These are used to run the long-running operations like external web-services call and bulk database operation asynchronously.
  2. If you want to run an apex class as a queueable apex then the Apex class has to implement a “queueable” interface.
  3. This interface adds jobs to the queue and observes them.
  4. Apex class has to define the execute method.
    .Syntax:public void execute(queueableContext qc){}
  5. Execute method in Queueable Apex is an Abstract method(with no definition) that execute the logic asynchronously.
  6. When we invoke the queueable job using the system.enquejob() method, this will return the id of the asynchronous job.using which we can track the status of the job.

Example: With Queueable apex update the Account records and assign the jobid to description field of account.

Apex Class:

public class QueuableExample implements Queueable {    
    public void execute(QueueableContext qc){          
        List<Account> acc=[select id,Email__c,fax,phone,description from Account where industry='banking'];       
        for(account a:acc){            
            a.phone='+5555555555';        
            a.Email__c='[email protected]';        
            string ids=qc.getJobId();       
            a.Description=ids;      
            update a;      
        }   
    }
} 

Anonymous Window: For Execution

QueuableExample qe = new QueuableExample();
system.enqueueJob(qe); 

Example:Calling schedule apex from Queueabe apex.

Queueable Apex:

public class ChainingApex2 implements Queueable
{       
    public void execute(queueablecontext qc1){        
        list<account> acc2=[select name,phone,industry,fax,sic from account where industry='banking'];     
        for(account b:acc2){          
            b.fax='143143143';     
            id x=qc1.getJobId();
            b.Sic=x;        
            update b;   
        }    
        ChainingApex1 ca=new ChainingApex1();    
        id ids=system.enqueueJob(ca);    
        Callschedule sa=new Callschedule();     
        string expression='0 34 16 7 12 ? 2017';   
        system.schedule('sctosc222',expression,sa);  
    }  
} 
public class chainingApex1 implements Queueable 
{  
    public void execute(queueablecontext qc){        
        list<account> acc1=[select name,phone,industry,fax,description from account where industry='banking'];     
        for(account a:acc1){           
            a.phone='9999999';        
            id x1=qc.getJobId();     
            a.description=x1;       
            update a;     
        }  
    } 
} 

Schedule Apex:

public class Callschedule implements schedulable 
{     
    public void execute(SchedulableContext sc){    
        list<Account> accs=[select id,name from account where name='Test1' limit 10]; 
        for(account a:accs){             
            a.name='Test1';    
            update a;     
        }   
    }
}

dont miss out iconCheck out another amazing blog by Kirandeep here: Future Methods in Salesforce: An Overview

GOVERNING LIMITS:

 Salesforce by default on every apex functionality imposed some limits to obey multi-tenant architecture. Governing Limits: 

  1. In every transaction maximum no. of SOQL statements: 100.
  2. In every transaction maximum no. of DML statements: 150. 
  3. In every transaction maximum no. of Records on which DML can be performed: 10,000.
  4. Every SOQL Query can return maximum: 50,000 Records. 
  5. In every transaction maximum no. of SOSL statements: 20. 
  6. In every transaction maximum no. of Future Methods: 50. 
  7. In every transaction maximum no. of Queueable call: 50. 
  8. In every transaction maximum no. of callouts: 100.
  9. In every transaction maximum no. of Email Invocations: 10

Popular Salesforce Blogs