Queueable Apex in Salesforce



 QUEUEABLE APEX IN SALESFORCE

  • Salesforce Has added Queueable Interface in Winter 15 release, which sounds like an alternative to the Future Method.
  • A queueable interface enables you to add jobs to the queue and monitor them. which is an enhanced way of running your asynchronous Apex code compared to Future Method. (Because Future method will not generate any ID of the Job to track).
  • Apex Process that runs for a long time such as complex database operation or external web service callouts, you can run them asynchronously by implementing the queueable interface and adding the job to the apex job queue.
  • We can track the job by using the "AsyncApexJob" Object. 
  • in this way, your asynchronous Apex job runs in the background in its own thread and doesn't delay the execution of your main Apex Logic.

A benefit of using the Queueable interface method is that some governor limits are higher than for synchronous Apex such as Heap size limit, Soql query limit.

SYNTAX FOR QUEUEABLE APEX

===============================================================
public class AsyncExecutionExample implements Queueable {

public void execute(QueueableContext context) {
Account a = new Account(Name='Acme',Phone='(415) 555-1212'); //BussinessLogic
insert a; } }

Invoking Queueable Apex

ID jobId = System.Enqueuejob(new AsyncExecutionExample() //ClassAnme);


 ================================================================

Queueable Apex Limit

  • You can add up to 50 jobs to the queue with System.enqueuejob in a single transaction.
  • No limit is enforced on the depth of chain job, which means that you can chain one job to another job and repeat this process with each new child job to link to the new child job.
Example For Queueable Apex

Use case:- Write an Apex program to implement queueable apex and implement the chaining of Job.


1. AccountQueueable (Job 1 to create an Account).
2. ContactQueueable (Job 2 to create a Contact).

Public class Accountqueueable implements Queueable{

    public void execute(QueueableContext context){
    Account acc = new Account();
    acc.Name = 'QueuAccount';
    acc.Rating = 'Hot';
    acc.Active__C = true;
    insert acc;
    if(acc!= null){
   
    //Chaning The queueable Apex (Calling the second queauble class means second Job)
    ID contactJobId = system.enqueuejob(new contactQueueable(acc));
   
    }
  }
}

===================================================================
Public class contactQueueable implements System.Queueable{
  Public Account accRecord;
  Public contactQueueable(Account acc){
    accRecord = acc;
  }
Public void Execute(system.queueablecontext context){
  if(accRecord 1 = null){
  contact con = New contact();
  con.lastName = 'contactqueue';
  con.Accountid = accRecord.id; // Map Account id while creating contact.
  insert contact
 
  }
 }
}
==========================================================================
Execution

Id accountJobID = system.enqueuejob(new Accountqueueable());
AsyncApexjob jobdetail = [Select id, Status from AsyncApexjob where Id = accountJobID];
system.debug('Job ID' +accountJobID);

===========================================================================


For more details please refer to the official link
Like our post & bookmark this blog for your future learning. 

Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 

Happy Coding Sharing Is caring. 😀😀😀 

إرسال تعليق

Post a Comment (0)

أحدث أقدم