+ Start a Discussion
steve pattinsonsteve pattinson 

Batch Apex to count processed records

Hello Techie's

How to know the number of records processed in a batch operation while working with five million records. Any Help would be appreciated.
Best Answer chosen by steve pattinson
NagendraNagendra (Salesforce Developers) 
Hi Steve,

Please find the below code snippet with an example how to count the number of records which are processed in batch operation.
 
public class BatchApexBL implements Database.batchable<Sobject>,Database.Stateful{
public integer count = 0;
public Database.QueryLocator start(Database.BatchableContext bc){
String str ='select id,Name,Price__c from Laptop__c';
return Database.getQueryLocator (str);
}

public void execute(Database.BatchableContext bc, List<Laptop__c>laps){
for(Laptop__c lap :laps){
lap.Price__c = lap.Price__c = 1500; 
}
 
try{
      
database.saveresult[] ds =  Database.update(laps,false);
    for(database.SaveResult d : ds){
        if(d.issuccess()){
            count++;
        }
        
    }
}
catch(exception e){
system.debug('update failed');
}
}

public void finish(Database.BatchableContext bc){
    system.debug(count);
//Send an email to the User after your batch completes
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'rakeshthotasfdc@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Batch Apex Job is done');
mail.setPlainTextBody('Total' +'  '+ count +'  '+ 'Records updated sucessfully');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}


Execution of batch job : 

BatchApexBL lapss = new BatchApexBL();
Database.executeBatch(lapss);

Please mark my solution as best answer if this helps you...............

Best Regards,
Nagendra.P

All Answers

VineetKumarVineetKumar
Make the batch class implment stateful interface, create a global static counter variable in your batch class.
Increment this value inside your processing loop.

Refer below link for code :
http://cloudisfuture.blogspot.ae/2012/11/batch-classes-in-salesforce.html
 
NagendraNagendra (Salesforce Developers) 
Hi Steve,

Please find the below code snippet with an example how to count the number of records which are processed in batch operation.
 
public class BatchApexBL implements Database.batchable<Sobject>,Database.Stateful{
public integer count = 0;
public Database.QueryLocator start(Database.BatchableContext bc){
String str ='select id,Name,Price__c from Laptop__c';
return Database.getQueryLocator (str);
}

public void execute(Database.BatchableContext bc, List<Laptop__c>laps){
for(Laptop__c lap :laps){
lap.Price__c = lap.Price__c = 1500; 
}
 
try{
      
database.saveresult[] ds =  Database.update(laps,false);
    for(database.SaveResult d : ds){
        if(d.issuccess()){
            count++;
        }
        
    }
}
catch(exception e){
system.debug('update failed');
}
}

public void finish(Database.BatchableContext bc){
    system.debug(count);
//Send an email to the User after your batch completes
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'rakeshthotasfdc@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Batch Apex Job is done');
mail.setPlainTextBody('Total' +'  '+ count +'  '+ 'Records updated sucessfully');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}


Execution of batch job : 

BatchApexBL lapss = new BatchApexBL();
Database.executeBatch(lapss);

Please mark my solution as best answer if this helps you...............

Best Regards,
Nagendra.P
This was selected as the best answer
sai tarunsai tarun
Thanks
Mr.Nagendra and Mr.VineetKumar.  :)