• Kapil_Khandelwal
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
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.
Hi,
I was wondering,  can we stop creating instances while there are instances already running of the apex class?

Thanks
Shweta.