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
ssoftwaressoftware 

Callout from scheduled Apex not supported errors

Hi All,

I am calling a batch class (with Database.AllowsCallouts) from a Scheduler class. The batch class is making http callouts. Both classes are using Apex version 27.0.

 

global class MyScheduler implements Schedulable {
global void execute(SchedulableContext sc) {

String query = 'Select .....';

MyBatchClass batchApex = new MyBatchClass(query);
ID bpid = Database.executeBatch(batchApex);

}

}

 

In my developer environment, the above code is working just fine. However when I deploy it to production (as part of a managed package) and run the scheduled class, I am getting the error:

"Callout from scheduled Apex not supported"

 

Why it works without issues in dev but not in production? Do I need to do anything in the production environment to get it working?


I have also tried the following variation of the scheduler class:

 

global class MyScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
runBatchProgram();
}

 

@future (callout=true)
static void runBatchProgram() {
String query = 'Select .....';

MyBatchClass batchApex = new MyBatchClass(query);
ID bpid = Database.executeBatch(batchApex);
}


}

 

With this I get the error: Database.executeBatch cannot be called from a batch or future method.

 

Any help is appreciated as I am stuck.

 

Madhav

Best Answer chosen by Admin (Salesforce Developers) 
ssoftwaressoftware

Hi All,

 

I am able to figure the solution. I moved a callout that was initially in the batch class contructor to the start method and this fixed the issue. Not sure why it worked in the dev environment initially. My guess why it works now is that "Database.AllowCallouts" comes into play after the batch class is constructed and works for start(), executeBatch(), and probably finish() methods.

 

Kind Regards

Madhav

All Answers

Bhawani SharmaBhawani Sharma
1. Make sure you implement Database.AllowCallouts on batch class.
2. You can't call and future method from batch or batch from a future as both are async request.
ssoftwaressoftware

Hi Bhawani,

 

Thank you for your reply. Yes, I am using Database.Callouts on the batch class - which makes it possible to call the batch class from a scheduler class.

 

Like I mentioned in the initial email, this is working in my DE but now in production. Any other ideas??

 

Thanks in advance

 

 

ssoftwaressoftware

Correction:

 

I meant this is working in my DE but not in production.

Bhawani SharmaBhawani Sharma
Have you added remote site setting?

What error you are getting in Production?
ssoftwaressoftware

Hi Bhawani,

 

Yes I have done so. I see the following error in the developer console when the scheduled class is run:

 

Callout from scheduled Apex not supported.

 

The batch class in question looks as follows:

global class MyBatchClass implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {

...
...
}

 

ssoftwaressoftware

Hi All,

 

I am able to figure the solution. I moved a callout that was initially in the batch class contructor to the start method and this fixed the issue. Not sure why it worked in the dev environment initially. My guess why it works now is that "Database.AllowCallouts" comes into play after the batch class is constructed and works for start(), executeBatch(), and probably finish() methods.

 

Kind Regards

Madhav

This was selected as the best answer
Sumit jaitlySumit jaitly
Hi All

I am facing this same issue, have you overcome on it? if yes then please share solution with me.

Regards
Sumit