7

I have the below sample code:

global class BatchApexExample implements Database.Batchable<SObject>{

    public Database.QueryLocator start(Database.BatchableContext bc){
        System.debug('Number of callouts allowed from Start method is '+ String.valueOf(Limits.getLimitCallouts()));
        return Database.getQueryLocator('SELECT Id,Name FROM Lead WHERE LastName = \'Batch Test\'');
    }

    public void execute(Database.BatchableContext bc, List<SObject> scope){
        System.debug('Number of callouts allowed from execute method are '+ String.valueOf(Limits.getLimitCallouts()));
        for(SObject obj: scope){
            Lead leadObj = (Lead)obj;
            leadObj.LastName = 'Batch Test Updated';
            update leadObj;
        }

    }

    public void finish(Database.BatchableContext bc){
        System.debug('Number of callouts allowed from finish method are '+ String.valueOf(Limits.getLimitCallouts()));
    }
}

I am just trying to see how many callouts are allowed from start, execute and finish method.

But this always prints as zero for all the three methods..What is wrong with this code?

Note: I have two leads in database with last name = 'Batch Test'

10
global class BatchApexExample implements Database.Batchable<SObject>,Database.AllowsCallouts {

public Database.QueryLocator start(Database.BatchableContext bc){
    System.debug('Number of callouts allowed from Start method is '+ String.valueOf(Limits.getLimitCallouts()));
    return Database.getQueryLocator('SELECT Id,Name FROM Lead WHERE LastName = \'Batch Test\'');
}

public void execute(Database.BatchableContext bc, List<SObject> scope){
    System.debug('Number of callouts allowed from execute method are '+ String.valueOf(Limits.getLimitCallouts()));
    for(SObject obj: scope){
        Lead leadObj = (Lead)obj;
        leadObj.LastName = 'Batch Test Updated';
        update leadObj;
    }

}

public void finish(Database.BatchableContext bc){
    System.debug('Number of callouts allowed from finish method are '+  String.valueOf(Limits.getLimitCallouts()));
  }
}

You will have to implement Database.AllowCallouts for batch jobs to make callouts .Please check the above code that I modified to see callout limits .

I tested your code with Database.AllowsCallouts and you will have 100 callouts in start,each execute method and finish method

enter image description here

4
  • 1
    I feel stupid now. – javanoob Aug 9 '15 at 2:33
  • 2
    Happens !!Take it easy :) – Mohith Shrivastava Aug 9 '15 at 2:45
  • In your screenshot it's still zero Number of callouts: 0 out of 0. Why is it so? – Eduard Oct 18 '18 at 11:51
  • because i do not make callouts there – Mohith Shrivastava Oct 25 '18 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.