function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumant KuchipudiSumant Kuchipudi 

Batch execute on Http callouts

Hi,
I have an object with 1.5 million records and I need to update couple of fields on all 1.5 m records by calling HTTP callout for each records, i.e. inside batch I need to call outside for 1.5 m times. Is that ok to run the query on all these records and invoke HttP callout? Is that feacible? whats the best procedure. 
Thank you very much.
Raj VakatiRaj Vakati
You can do it .. but it may take your entire ORG  API limits .. 

This is what i can  sugggest is Create a batch that will update the data as like below 
  1. Run Your batch Runs every houts  and update protation of the recrods 
  2. when next batch runs update the records that not run run by previus batch ( in where condition you can check the field that )


This is how you can do it .. but if you do it one time for all the records .. it may be an issue. 
Raj VakatiRaj Vakati
You can do refer this class 
global class BatchSync implements Database.Batchable<sObject>,   Database.AllowsCallouts {

 public String query = 'Select ID, Name from Account';
 global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(query);
 }

     global void execute(Database.BatchableContext BC, List<Account> records) {         
        String endpoint;        

        for ( integer i = 0; i< records.size(); i++ ){
         try {                  
          HttpRequest req = new HttpRequest();
          HttpResponse res = new HttpResponse();
          Http http = new Http();
          // Set values to Params

          endpoint = 'Your endpoint';

          req.setHeader('Authorization', header);
          req.setHeader('Content-Type', 'application/json');
          req.setEndpoint(endpoint);
          req.setMethod('POST');
          req.setBody('Information you wanna send');
          req.setCompressed(true); // This is imp according to SF, but please check if
                                 // the webservice accepts the info. Mine did not :P
                                 // Had to set it to false

          if (!Test.isRunningTest()) {      
            res = http.send(req);
            String sJson = res.getBody();
            System.debug('Str:' + res.getBody());
          }             
          // now do what u want to with response.               
          }
          catch (Exception e) {         
            System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );           
          }
       }
    }   

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