Webservice callout from Scheduled Apex
Making webservice callout from Schedulable classes
Ever faced the error "System.CalloutException: Callout from scheduled Apex not supported."
This is the error you get when you are making a webservice callout from a class which is implementing Database.Schedulable interface. Salesforce does not allow you to make callouts from Schedulable classes. So I had this requirement where I needed to schedule a piece of code which was making a callout to external websrvice.
This is my schedulable class :
public class ExampleScheduler implements Schedulable{
public void execute(SchedulableContext SC) {
ExampleHelper.makeWebserviceCallout();
}
}
This is the class which contains webservice callout :
public class ExampleHelper{
public static void makeWebserviceCallout(){
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.yahoo.com');
req.setMethod('GET');
String username = 'myname';
String password = 'mypwd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
}
}
When the scheduled class runs, it throws an error "System.CalloutException: Callout from scheduled Apex not supported."
To resolve this, you can implement either of the two below mentioned solutions :
Solution# 1 : Your scheduler class should implement Database.Batchable and Database.AllowsCallouts
1) Implement Batchable interface - We will only use the start method. Rest of the methods would be empty
2) Implement Database.AllowsCallouts - If we implement this inteface, we can make callouts from batch class
The updated class would look like :
public class ExampleScheduler implements Schedulable, Database.AllowsCallouts, Database.Batchable<sObject> {
public void execute(SchedulableContext SC) {
Database.executebatch(new ExampleScheduler());
}
public Iterable<sObject> start(Database.Batchablecontext BC){
ExampleHelper.makeWebserviceCallout();
return null;
}
public void execute(Database.BatchableContext BC, List<sObject> scope){
}
public void finish(Database.BatchableContext info){
}
}
Solution# 2 : Create a class which implements Queueable, Database.AllowsCallouts
1) Queueable interface : This interface enables you to add jobs to the queue and monitor them, which is an enhanced way of running your asynchronous Apex code compared to using future methods.
2) Database.AllowsCallouts : This interface allows you to make callouts from Queueable and Batchable classes
public class ExampleScheduler implements Schedulable{
public void execute(SchedulableContext SC) {
System.enqueueJob(new ExampleQueueable());
}
}
public class ExampleQueueable implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
ExampleHelper.makeWebserviceCallout();
}
}
Ever faced the error "System.CalloutException: Callout from scheduled Apex not supported."
This is the error you get when you are making a webservice callout from a class which is implementing Database.Schedulable interface. Salesforce does not allow you to make callouts from Schedulable classes. So I had this requirement where I needed to schedule a piece of code which was making a callout to external websrvice.
This is my schedulable class :
public class ExampleScheduler implements Schedulable{
public void execute(SchedulableContext SC) {
ExampleHelper.makeWebserviceCallout();
}
}
This is the class which contains webservice callout :
public class ExampleHelper{
public static void makeWebserviceCallout(){
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.yahoo.com');
req.setMethod('GET');
String username = 'myname';
String password = 'mypwd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
}
}
When the scheduled class runs, it throws an error "System.CalloutException: Callout from scheduled Apex not supported."
To resolve this, you can implement either of the two below mentioned solutions :
Solution# 1 : Your scheduler class should implement Database.Batchable and Database.AllowsCallouts
1) Implement Batchable interface - We will only use the start method. Rest of the methods would be empty
2) Implement Database.AllowsCallouts - If we implement this inteface, we can make callouts from batch class
The updated class would look like :
public class ExampleScheduler implements Schedulable, Database.AllowsCallouts, Database.Batchable<sObject> {
public void execute(SchedulableContext SC) {
Database.executebatch(new ExampleScheduler());
}
public Iterable<sObject> start(Database.Batchablecontext BC){
ExampleHelper.makeWebserviceCallout();
return null;
}
public void execute(Database.BatchableContext BC, List<sObject> scope){
}
public void finish(Database.BatchableContext info){
}
}
Solution# 2 : Create a class which implements Queueable, Database.AllowsCallouts
1) Queueable interface : This interface enables you to add jobs to the queue and monitor them, which is an enhanced way of running your asynchronous Apex code compared to using future methods.
2) Database.AllowsCallouts : This interface allows you to make callouts from Queueable and Batchable classes
public class ExampleScheduler implements Schedulable{
public void execute(SchedulableContext SC) {
System.enqueueJob(new ExampleQueueable());
}
}
public class ExampleQueueable implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
ExampleHelper.makeWebserviceCallout();
}
}
Comments
Post a Comment