+ Start a Discussion
SFDC ROCKSFDC ROCK 

Is any another way to call future method in batch apex?

Is any another way to call future method in batch apex? As we know that we can not call from one asynchronous process from another asynchronous process.
Ahmed Ansari 13Ahmed Ansari 13

For this work around you go through link below
https://developer.salesforce.com/forums/?id=906F0000000AqdVIAS

 

Raj VakatiRaj Vakati
Salesforce doesn't allow a future method to be called from another future method or a batch job. Before calling your future method, you should check if a future or batch job is already running. This would be a best practice for any code you ever write that calls a future method. Good news is that it's very easy. Example below: 

if(System.IsBatch() == false && System.isFuture() == false){ 
    // make your future call here 
}
Maharajan CMaharajan C
Hi Subodh,

Just try is that working or not.

Use the platform Event here.

Insert the Platform Event from the Batch class and from Platform Event try to execute the Future Method.

Thanks,
Maharajan.C
Ajay K DubediAjay K Dubedi
Hi Subodh,

All future methods and Batch Classes are Asynchronous and we can not call an Asynchronous call from an Asynchronous call.
Yes, you can call (A queueable class method acts as a future method) only from the Batch class finish method. You can implement a Queueable class that acts as a Future method and then you can invoke another future method. This is an indirect way of calling a future method from the future method (A queueable class method acts as a future method). But this approach also has a governor limit of 2.
So at max, you can call two future methods.
Batch Class:
global class NewBatchClass implements Database.Batchable<Account>,Database.AllowsCallouts{
 global Iterable<Account> start(Database.BatchableContext bc) {
    return [select id,name from account ];
 }

    global void execute(Database.BatchableContext BC, List<Account> scope)
        {
            
        }

    global void finish(Database.BatchableContext BC)
        {
            
                TestBatch tt = new TestBatch();
            System.enqueueJob(tt);
            
            
        }

}
Future-Queuable class:
public with Sharing class TestBatch implements Queueable, Database.AllowsCallouts {


    public void execute(QueueableContext context) {
    }

}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi