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
SennahSennah 

Callout from scheduled Apex not supported

Dear Product Manager in charge for the "Apex Scheduler",

 

I was wondering why my code did not worked when scheduled. After requesting the debug logs, I could see the following:

 

 

EXCEPTION_THROWN|[45,19]|System.CalloutException: Callout from scheduled Apex not supported.

 

Here are my questions:

 

1. Is this going to be officially changed?

2. Is there a workaround?

3. Did I missed something in the documentation or is it simply not written down there?

 

Quite disappointed,

 

Hannes

 

Best Answer chosen by Admin (Salesforce Developers) 
cloudcodercloudcoder
You need to use the @future annotation. See http://blog.sforce.com/sforce/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html for an example

All Answers

cloudcodercloudcoder
You need to use the @future annotation. See http://blog.sforce.com/sforce/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html for an example
This was selected as the best answer
SennahSennah

Of course! Thanks for the hint.

I had that added in my Sandbox, but not deployed yet. Don't know what happened.

 

Best,

Hannes

Chirag MehtaChirag Mehta

Thanks for the hint, it helped!

fgwarb_1fgwarb_1

FYI, that link is currently returning a page with the message below, I might be suffering from copy/paste failure, or maybe it's a temporary issue? 

 

Heroku | No such app

There is no app configured at that hostname.
Perhaps the app owner has renamed it, or you mistyped the URL.

 

 

Anyway, for future googler's the design pattern below worked for me.

 

There are some design decisions that you'll need to make for yourself when using this.  My test approach might not be necessary for your implementation, I included it as without it I was hitting the "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out" error because the system.schedule() method runs "DML" on the CronTrigger table which blocks the callout.

 

I'm interested in more elegant solutions than the "if(! test.isRunningTes())" approach if anyone has any.

 

 

global class className implements Schedulable{
    global void execute(SchedulableContext sc){
        blah();
    }
    public static void blah(){
        blah();
    }
    @future(callout=true)
    public static void blah(Object fakeResponse){
        //do whatever prep is necessary
        Object response = fakeResponse;
        if(! test.isRunningTest()) response = actualCallout();
    }
}


@isTest
public class test_className{
    public static testMethod void test(){
        test.startTest();
            system.schedule('jobName',cron_expression, class);
        test.stopTest();
    }

}