How to make HTTP Callout in Batch Apex class Salesforce

by Rijwan Mohmmed
how-to-make-http-callout-in-batch-apex-class-salesforce

To call an HTTP callout in batch Apex, we need to implement Database.AllowsCallouts interface in the class. if we do not use this then an error will populate “callout not allowed”. We can make 100 callouts in a transaction of batch class. So always remember about the callout limit in apex class. because there will populate an error “System.LimitException: Too many callouts: 101” about 100 callouts. So batch size should be always below 100.

HttpBatchApex.cls :

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

    global String query = 'SELECT Id, LastName FROM Contact  LIMIT 100';
	 
    global Database.QueryLocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	 }

     global void execute(Database.BatchableContext BC, List<Contact> scope) {         
        String endpoint;        
	for(Contact cont : scope){
         try {                  
	      HttpRequest req = new HttpRequest();
	      HttpResponse res = new HttpResponse();
	      Http http = new Http();
				  
	      endpoint = 'Your endpoint url';

	      req.setHeader('Authorization', header);
	      req.setHeader('Content-Type', 'application/json');
	      req.setEndpoint(endpoint);
	      req.setMethod('POST');
	      req.setBody('Information you wanna send');
	      res = http.send(req);
	      String sJson = res.getBody();
	      System.debug('Str:' + res.getBody());
          }
          catch (Exception e) {         
            System.debug('Error:' + e.getMessage() + 'Line no:' + e.getLineNumber() );           
          }
       }
    }   

    global void finish(Database.BatchableContext BC){}
}

Call Batch Class :

HttpBatchApex httpBatch = new HttpBatchApex();
Id batchJobId = Database.executebatch(httpBatch, 100);
What’s your Reaction?
+1
8
+1
2
+1
0
+1
0
+1
0
+1
1

You may also like

3 comments

WindBoy July 20, 2023 - 6:25 am

This doesn’t exactly work for me
But I got a hint that like “Database.AllowsCallouts” can solve “Too many callouts” in Batch
and Database.executebatch(httpBatch, 100); => 100 can change amount about the batch split works
So I think this is greate post 🙂

I think this post is not worth to me for pay 1 coffee
But you have much posts
when i get 1 exact advice or 2 more hint like this in another post I’m willing to offer coffee

Thank you for the nice post

Reply
Rijwan Mohmmed July 20, 2023 - 2:53 pm

Hi @WindBoy, I think you missed last part of this. should below 100 batch size.
because there will populate an error “System.LimitException: Too many callouts: 101” about 100 callouts. So batch size should be always below 100.

Reply
WINDBOY July 21, 2023 - 2:55 pm

Thank you for the nice advice
But I did not need to use 100+callouts in batch

I trying to use 1 callout in Batch And It makes Error “Too many callouts: 1”
and it can solved with “Database.AllowsCallouts”

Anyway, thanks again for pointing out an important point.
I was able to confirm the part I had overlooked this time.

Have a Nice day RIJWAN MOHMMED!!

Reply

Leave a Comment