Error System.CalloutException: Callout from scheduled Apex not supported

i have created a schedulable class which has to trigger a batch job. The run method for the batch class will get list of data to process from a webservice callout. Now, i got the exception below. Any suggestions please.

System.CalloutException: Callout from scheduled Apex not supported.

global class scheduledDeltaBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
    ID batchprocessid = BatchUpsertSAPCustomerMasterBPSync.run(SAPServices.GetSAPCustomerMasterBPSync('Delta'));
   }
}

Answer

If you want to do it in the schedule class you will need to do it in an @future method:

https://developer.salesforce.com/blogs/developer-relations/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

or here on stack exchange:

Scheduled Apex, Callouts and Running a Batch

Why not just perform the callout in your Batch Apex start method? This
means you will always start the Batch Apex job from your Scheduled
apex job, but i think is on balance better than having to marshall
schedule jobs between @future jobs?

if you want to do it within the batch that is ran by the schedule you will need to implement the callout interface

global class scheduledDeltaBatchable implements Schedulable, Database.AllowsCallouts

To use a callout in batch Apex, specify Database.AllowsCallouts in the
class definition

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Attribution
Source : Link , Question Author : Bforce , Answer Author : Community

Leave a Comment