Salesforce Asynchronous process in one place


Program-1:Calling Future method from Batch apex

Global class MassDeleteBatch implements Database.Batchable<sobject>
{
   //Calling @future method from Constructor 
    global MassDeleteBatch()
    {       
        fuMethod1();   
 fuMethod2();
 //We can call upto 50 future methods 
    }
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([Select Id from Account]);
    }
    global void execute(Database.BatchableContext BC, List<SObject> lstRecords)
    {
  //Your business logic goes here 
    }
    global void finish(Database.BatchableContext BC) { }
    
    @future(callout=true)
    public static void fuMethod1()
    {
        //Callout logic inside the @future method'
    }
 @future
    public static void fuMethod2()
    {
        //Your logic inside the @future method'
    }
}
Note: Only thing is we can't call @future method from start(), execute(),finish() methods. If we do then we will get the "First error: Too many future calls: 1" error.
Program-2: Calling Batch class from Future Method
Global class BatchExample implements Database.Batchable<sobject>
{ 
    global Database.QueryLocator start(Database.BatchableContext BC)
    {        
        return Database.getQueryLocator([Select Id from Account]);
    }
    global void execute(Database.BatchableContext BC, List<SObject> lstRecords)
    {  
  //batch logic here
    }
    global void finish(Database.BatchableContext BC) { 
 }   
}
//-----------------------------------------------
public class BaseClass 
{
    @future
    public static void toCallBatchClass()
    {
        Database.executeBatch(new MassDeleteBatch());
    }
}

//----------Executing From Anonymous Window--------------
BaseClass.toCallBatchClass();
The above program will throw the below error :
First error: Database.executeBatch cannot be
called from a batch start, batch execute, or future method.
Program-3:Calling Future method from Future method
public class BaseClass 
{
    @future
    public static void toCallAnotherFuture()
    {
        futureMethod();
    }
    @future
    public static void futureMethod()
    {
        //Your logic here
    }
}

//----------Executing From Anonymous Window--------------
BaseClass.toCallAnotherFuture();
The above program will throw the below error :
First error: Future method cannot be called
from a future or batch method: BaseClass.futureMethod()
Program-4:Calling Queueable class from Future method
public class BaseClass 
{
    @future
    public static void toCallQueueableClass()
    {
       System.enqueueJob(new QblClass());
    }    
}
//-----------------------------------------------
public class QblClass  implements Queueable {
    public void execute(QueueableContext context)
    {
    }
}

//----------Executing From Anonymous Window--------------
BaseClass.toCallQueueableClass();
Program-5: Calling Batch apex from Queueable Class
public class QblClass  implements Queueable {
    public void execute(QueueableContext context)
    {
        Database.executeBatch(new BatchExample());
    }
}
//-----------------------------------------------
Global class BatchExample implements Database.Batchable<sobject>
{ 
    global Database.QueryLocator start(Database.BatchableContext BC)
    {        
        return Database.getQueryLocator([Select Id from Account]);
    }
    global void execute(Database.BatchableContext BC, List<SObject> lstRecords)
    {  
  //batch logic here
    }
    global void finish(Database.BatchableContext BC) { 
 }   
}
//----------Executing From Anonymous Window--------------
System.enqueueJob(new QblClass());
Program-6: Calling Future method from Queueable Class
public class QblClass  implements Queueable {
    public void execute(QueueableContext context)
    {
       toCallQueueableClass();
    }
    @future
    public static void toCallQueueableClass()
    {
       
    }    
}
//----------Executing From Anonymous Window--------------
System.enqueueJob(new QblClass());

 tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments

  1. thanks manjunath, your articles are crispy and to the point...very much useful..

    ReplyDelete
  2. Hi I want to talk about first point where you mentioned future can be called from a batch constructor. But this is not the actual future signature. future method must accept the parameter like collection of primitive etc.
    Apart from this you have done really a great job. please keep posted so that people can get more kknowledge.
    thanks

    ReplyDelete
  3. Thank you for sharing this information, It is really helpful and an easy way to understand.

    ReplyDelete
  4. Note: Only thing is we can't call @future method from start(), execute(),finish() methods. can you correct it even from finish method too we can't call it

    ReplyDelete

Post a Comment