Database.Allowscallouts allows us to use a callout in batch Apex.Callouts include HTTP
requests as well as methods defined with the webservice keyword.
How to make webservice callout from scheduler
scenario-1 : if scheduler doesn't depends on Batch or Queueable(depends on future methods)
scenario-2 : If Scheduler depends on Batch
scenario-3 : If Scheduler depends on Queueable
Note :
if we try to make a call from scheduler,we will get the below exception:
system.calloutException: callout form scheduled Apex not supported
Reason : All we know that we cannot schedule a class which contains callout.
The only solution for this is to use @future annotation to enforce(for Normal apex classes)
but we cannot process future methods inside batch.
Scenario -1 :
if scheduler doesn't depends on Batch or queueable
ex :
//Scheduled Apex
public class DemoScheduler1 implements Schedulable{
public void execute(SchedulableContext sc){
system.debug('*******Going to call future method ');
DemoAsynchronousTest.futureMethodCallFromScheduler();
}
}
//apex class containing future method
public class DemoAsynchronousTest{
@future
public static void futureMethodCallFromScheduler(){
system.debug('******futureMethodCallFromScheduler get called');
}
}
Scenario -2 :
if scheduler depends on Batch
ex :
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){
}
}
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());
}
}
Scenario-3 :
if scheduler depends on queueable
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();
}
}
or
global class SampleCalloutCls implements Queueable, Schedulable, Database.AllowsCallouts{
//variable declaration
private List<Account> accountList ;
//Queueable interface method
public void execute(QueueableContext QC){
// Perform callout here to get the accounts from external system.
//Calling batch process if list contains records.
if(!accountList.isEmpty()){
Database.executeBatch(new SampleBatchCls(accountList));
}
}
//Method to Schedule the current class
global void execute(SchedulableContext sc) {
system.enqueueJob(new SampleCalloutCls());
}
}
//End of class
requests as well as methods defined with the webservice keyword.
How to make webservice callout from scheduler
scenario-1 : if scheduler doesn't depends on Batch or Queueable(depends on future methods)
scenario-2 : If Scheduler depends on Batch
scenario-3 : If Scheduler depends on Queueable
Note :
if we try to make a call from scheduler,we will get the below exception:
system.calloutException: callout form scheduled Apex not supported
Reason : All we know that we cannot schedule a class which contains callout.
The only solution for this is to use @future annotation to enforce(for Normal apex classes)
but we cannot process future methods inside batch.
Scenario -1 :
if scheduler doesn't depends on Batch or queueable
ex :
//Scheduled Apex
public class DemoScheduler1 implements Schedulable{
public void execute(SchedulableContext sc){
system.debug('*******Going to call future method ');
DemoAsynchronousTest.futureMethodCallFromScheduler();
}
}
//apex class containing future method
public class DemoAsynchronousTest{
@future
public static void futureMethodCallFromScheduler(){
system.debug('******futureMethodCallFromScheduler get called');
}
}
Scenario -2 :
if scheduler depends on Batch
ex :
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){
}
}
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());
}
}
Scenario-3 :
if scheduler depends on queueable
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();
}
}
or
global class SampleCalloutCls implements Queueable, Schedulable, Database.AllowsCallouts{
//variable declaration
private List<Account> accountList ;
//Queueable interface method
public void execute(QueueableContext QC){
// Perform callout here to get the accounts from external system.
//Calling batch process if list contains records.
if(!accountList.isEmpty()){
Database.executeBatch(new SampleBatchCls(accountList));
}
}
//Method to Schedule the current class
global void execute(SchedulableContext sc) {
system.enqueueJob(new SampleCalloutCls());
}
}
//End of class
No comments:
Post a Comment