3

I have scheduled the execution of my class and am getting an error. What did I do wrong and how do I fix it?

global class SchedulableExchangeRate implements Schedulable {
    global void execute(SchedulableContext ctx){
        CurrencyExchangeController newCurExcContr = new CurrencyExchangeController();
        newCurExcContr.calloutCurrency();
    }
}


public class CurrencyExchangeController {
    public void calloutCurrency(){
    //here is the code of my method
    }
}

1 Answer 1

10

You're not allowed to make a callout from a Scheduled job. To fix this, call a future or queueable from the scheduled job.

global class SchedulableExchangeRate implements Schedulable {
    global void execute(SchedulableContext ctx){
        CurrencyExchangeController.calloutCurrency();
    }
}


public class CurrencyExchangeController {
    @future(callout=true) public static void calloutCurrency(){
    //here is the code of my method
    }
}

Note the change to a static method, and (callout=true). Note also you're limited to just one @future method in a schedulable execution.

3
  • I have one scheduler apex, one flow, and one apex to make integration, the scheduler calls the flow, and the flow calls the apex of integration, in this case, should give an error of "Callout from scheduled Apex not supported"? Commented Sep 21, 2022 at 18:38
  • 1
    @ArthurAlmeida Yes, if you're still in schedulable context (System.isScheduled() == true), you'll get that exception. That Apex class needs to call a future or queueable method.
    – sfdcfox
    Commented Sep 21, 2022 at 19:57
  • 1
    in my case, this error sometimes occurs, and sometimes no... it does not make sense to me... Commented Sep 21, 2022 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .