function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kapil_KhandelwalKapil_Khandelwal 

Future method cannot be called from a future or batch method: ExampleHelper.makeWebserviceCallout()

I want to schedule an apex class using System.schedule.

This is the class I need to schedule. (ExampleHelpera.pxc)
public class ExampleHelper {
    
    @future(callout=true)
    public static void makeWebserviceCallout(){
        
       //logic goes here.

   }
}

This is the class that implements schedulable and calls the apex class. (ScheduleTest.apxc)
public class ScheduleTest implements Schedulable, Database.AllowsCallouts, Database.Batchable<sObject> {
    
    public void execute(SchedulableContext SC) {
        Database.executebatch(new ScheduleTest());
    }
    
    public Iterable<sObject> start(Database.Batchablecontext BC){
        if( !System.isFuture()) {
            ExampleHelper.makeWebserviceCallout();
        }
        
        
        return null;
    }
    
    public void execute(Database.BatchableContext BC, List<sObject> scope){  
        
    }
    
    public void finish(Database.BatchableContext info){
        
    }
    
}


Now I try Schedule the above code using following:
 
ScheduleTest st = new ScheduleTest();
String sch=  '0 20 18 16 8 ?';
String jobId=System.schedule('Test Schedule', sch, st);

After running the above code snippet in Execute Anonymous Window, the schedule is being created (as I had verified it Setup > jobs > Scheduled jobs)
But the logic inside the apex class is not being executed.
I get the following error - Future method cannot be called from a future or batch method: ExampleHelper.makeWebserviceCallout()

But when I run my logic code in Execute Anonymous Window, it gets executed without any error.


Kindly help me out with the same.
Thanks in advance.
Raj VakatiRaj Vakati
use Queueable can be called from Database.Batchable classes.

 
public class ExampleHelper implements Queueable,Database.AllowsCallouts, {
    
    public void execute(QueueableContext context) {
         // Web service calls
    }
	
}
Batch CLass
public class ScheduleTest implements Schedulable, Database.AllowsCallouts, Database.Batchable<sObject> {
    
    public void execute(SchedulableContext SC) {
        Database.executebatch(new ScheduleTest());
    }
    
    public Iterable<sObject> start(Database.Batchablecontext BC){
        System.enqueueJob(new ExampleHelper());
        
        
        return null;
    }
    
    public void execute(Database.BatchableContext BC, List<sObject> scope){  
        
    }
    
    public void finish(Database.BatchableContext info){
        
    }
    
}

 
Kapil_KhandelwalKapil_Khandelwal
@Raj, Thank you for your response.

I tried to implement it using Queueable, but now I am getting another error:

First error: Start did not return a valid iterable object.

 
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below post
1) http://amitsalesforce.blogspot.com/2017/08/webservice-callout-from-scheduled-apex.html
 
global class ScheduleTest implements Schedulable{
    global void execute(SchedulableContext sc) 
    {        
       ExampleHelper.makeWebserviceCallout();
    }    
}
public class ExampleHelper {
    
    @future(callout=true)
    public static void makeWebserviceCallout(){
        
       //logic goes here.

   }
}

Let us know if this will help you