+ Start a Discussion
raju gangaraju ganga 

how to count the number of records processed with batch jobs ?

Suraj GharatSuraj Gharat

Make your batch statefull and then add a varibale that would hold an aggregate sum of all records processed by every batch execution, or just set it to the size of what you return from "start" method.
Amit Chaudhary 8Amit Chaudhary 8
Hi Raju,

You can Implement Database.Stateful, count records in execute method and you can get total count in finish method. Here is a sample
 
global class InvoiceBatch implements Database.Batchable<sObject>,Database.Stateful{

   global integer count = 0;

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

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        for(sobject s : scope){
            count++;
            //Process records
        }   
    }

   global void finish(Database.BatchableContext BC){
       System.debug('count:' + count);
   }
}

Please let us know if this will help u

Thanks
AMit Chaudhary
suneel sunkarasuneel sunkara
fantastic explanation Amit sir
suryansh guptasuryansh gupta
http://forcedotcom-solutions.blogspot.in/2018/04/using-state-in-batch-apex.html