
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:-
Don't forget to check: How to Write a Batch Apex in Salesforce
Queueable is a interface whereas future is a method.
- These are used to run the long-running operations like external web-services call and bulk database operation asynchronously.
- If you want to run an apex class as a queueable apex then the Apex class has to implement a “queueable” interface.
- This interface adds jobs to the queue and observes them.
- Apex class has to define the execute method.
.Syntax:public void execute(queueableContext qc){} - Execute method in Queueable Apex is an Abstract method(with no definition) that execute the logic asynchronously.
- 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; } } }
Check 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:
- In every transaction maximum no. of SOQL statements: 100.
- In every transaction maximum no. of DML statements: 150.
- In every transaction maximum no. of Records on which DML can be performed: 10,000.
- Every SOQL Query can return maximum: 50,000 Records.
- In every transaction maximum no. of SOSL statements: 20.
- In every transaction maximum no. of Future Methods: 50.
- In every transaction maximum no. of Queueable call: 50.
- In every transaction maximum no. of callouts: 100.
- In every transaction maximum no. of Email Invocations: 10