HttpCallOut from a batch


Jan 04, 2018    Janaki Mahapatra, Salesforce


global class  BatchName implements Database.Batchable,Database.AllowsCallouts {

public String uri;
    public String requestUri;

    public BatchName(String requestUri){
        requestUri = requestUri;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Your query goes here';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC,  List scope){

        /*
        Call Your Http CallOut Service Class
         */
        MyServiceClass srv = new MyServiceClass(this.requestUri);
        Map jsonMap= srv.YourMethod();
        
        for (YourObject obj : scope){
            obj.FieldName = String.valueOf(jsonMap.get('keyName'));
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC){

    }
}
Remember to mention Allowcallouts=true on batch context then you can do callout in batch job