9

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'

1 Answer 1

13
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 a 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 at start,each execute method and finish method

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .