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' } }
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();
First error: Database.executeBatch cannot be
called from a batch start, batch execute, or 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();
First error: Future method cannot be called from a future or batch method: BaseClass.futureMethod()
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();
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());
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 🌝
thanks manjunath, your articles are crispy and to the point...very much useful..
ReplyDeleteThank you Srinivas
ReplyDeleteHi 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.
ReplyDeleteApart from this you have done really a great job. please keep posted so that people can get more kknowledge.
thanks
Thank you for sharing this information, It is really helpful and an easy way to understand.
ReplyDeleteThank you for the feedback.
DeleteNote: 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