Batch Apex With Webservice Callouts

To make an API call from batch apex, specify Database.AllowsCallouts in the class.

Example to make webservice callout in batch apex.

global class AccountBatchApex implements Database.Batchable<sObject>, Database.AllowsCallouts{
 global Database.QueryLocator start(Database.BatchableContext bc){
 String soqlQuery = 'SELECT Name, AccountNumber, Type From Account';
 return Database.getQueryLocator(soqlQuery);
 global void execute(Database.BatchableContext bc, List<Account> scope){
 for (Account acc : scope){
 if(acc.Type.equals('Customer - Direct')){
 HttpRequest request = new HttpRequest();
 HttpResponse response = new HttpResponse();
 Http http = new Http();
 String username = 'YourUsername';
 String password = 'YourPassword';
 Blob headerValue = Blob.valueOf(username + ':' + password);
 String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
 request.setHeader('Authorization', authorizationHeader);
 request.setHeader('Content-Type', 'application/json');
 request.setEndpoint('Your Endpoint URL');
 request.setBody('Information to Send');
 response = http.send(request);
 if (response.getStatusCode() == 200) {
 String jsonResponse = response.getBody();
 System.debug('Response-' + jsonResponse);
 System.debug('Error-' + e.getMessage());   
 global void finish(Database.BatchableContext bc){

Note: Total number of callouts (HTTP requests or Web services calls) is 100, that means if you have one callout in your execute method you can keep batch size as 100.

1 view0 comments

Recent Posts

See All

Salesforce Configuration Topics

CRM - Customer Relationship Management 1.             What is CRM(Customer Relationship Management)? 2.             CRM Basic entities 3.             Sub Modules of CRM 4.             Existing CRMs Sa

Daily Information | Daliylinx are related to General information, Technology, Salesforce, Newspapers, Telugu Newspapers, Pets, Cars etc

  • Facebook