I am receiving an error message when trying to test a class that implements Schedulable.

System.CalloutException: Callout from scheduled Apex not supported.

People have suggested using the @future method, but I am worried about my application being out of sync. I am using a callout to synchronize a Salesforce object to another system and then update the Salesforce object with the ID from the other system. My though is that the @future method may prevent that from happening in a linear fashion (SOQL to get sObject, Callout to create and get the ID from other system, Update sObject with ID).

I have looked at using a Database.Batchable interface but am a bit lost. All I need the batch to do is the following.

bigClass sync = new bigClass();

Batchable's execute method appears to require a List. Is there any way to have it just run the init method?

  • what 'launches' the schedulable class? What kind of object (business purpose) requires the sync?
    – cropredy
    Feb 4 '16 at 1:55

I'm pretty sure that this will work (assuming that you don't need to schedule back to back callouts as you can only have one Queueable Callout running at a time)

public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        //Do your 
        //SOQL to get sObject, 
        //Callout to create and get the ID from other system, 
        //Update sObject with ID
        //It can all happen in this one method.

In your scheduled class, just instantiate the above Class and execute it!

public class ScheduledClass implements Schedulable {
    public void execute(SchedulableContext SC) {
        System.enqueueJob(new AsyncExecutionExample());

I think that's all you have to do.

  • Awesome! Code Coverage tests ran and it seems like it will work! Now to move to production and make sure. Definitely adds a layer of complexity... You'd think Salesforce would just allow the Schedulable Interface to be run asynchronously and allow callouts, but I digress. Thanks! Feb 4 '16 at 17:19
  • 1
    Yeah, it's kind of a pain eh. However, compared to what you have to do to implement a batchable version, it's a lot cleaner :) Feb 4 '16 at 19:19
  • And wouldn't you know it! There is a known issue with Queueable and Mock Callouts - they're tripping the callout loop detector on unit tests. Our Sandbox is API version 36 so I didn't catch it until I created a Change Set. Oh well... Now to wait until next Saturday. success.salesforce.com/issues_view?id=a1p30000000sXtbAAE Feb 4 '16 at 22:57
  • Lucky the fix is coming so soon! Still, next Saturday is going to FEEL like a way off... Feb 4 '16 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.