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
Ravindra reddy MRavindra reddy M 

count the batch values in execute method

HI All,
i have 5 batches each batch containg 200 records now i need to count the records each batch
suppose batch 1 have 200 records and batch 2 contain 200  records now the total is 400 
i need to display total count. how to achive this ,plz help me to slove 
thanks In advance
Thanks & Regards
Raveendra
Raj VakatiRaj Vakati
Hi Ravindra,
You need to do it with Stateful interface. refer this code
global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

   global final String Query;
   global integer count;
  
   global SummarizeAccountTotal(String q){Query=q;
     count = 0;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext BC, List<sObject> scope){
      for(sObject s : scope){
		  count =count+1 ;
   }

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