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
Pradeep dPradeep d 

how to count total number of records in batch apex

wanted to count total number of records 

public List<Contact> contactsize = new List<Contact>();
     global void execute(Database.BatchableContext bc,  List<Contact> Scope){
          conupdate = new List<Contact>();
       system.debug('calling update check execute');
    
       For(Contact c: Scope){  
            c.Direct_send__c= true;
              conupdate.add(c);   
           }
       system.debug('contact list' + conupdate.size());  
         contactsize.addAll(conupdate);
         update conupdate;
     }  
    global void finish(Database.BatchableContext bc){
     system.debug('contacts size :' + contactsize.size());
        recordcount = contactsize.size();
Best Answer chosen by Pradeep d
v varaprasadv varaprasad
Hi Pradeep,

To avoid error check once below link : 
https://help.salesforce.com/articleView?id=000239885&type=1

Thanks
Varaprasad

All Answers

Abdul Ayan ChoudharyAbdul Ayan Choudhary
implement Database.Stateful,
Initialize an Integer count=0;
as
For(Contact c: Scope){ 
           c.Direct_send__c= true;
           conupdate.add(c);
           count++;
 }
count will retain its value between transactions.
Abdul Ayan ChoudharyAbdul Ayan Choudhary
count should be Instance Variable.
Pradeep dPradeep d

@Abdul Ayan

Database.stateful @ Heap memory issues are getting 

Pradeep dPradeep d

Am getting this error  with stateful 

 

First error: Batchable instance is too big: updatecheck

v varaprasadv varaprasad
Hi Pradeep,

Please check once below sample code : 
 
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);

Hope this helps you

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com



 
Pradeep dPradeep d
am getting this error First error: Batchable instance is too big: updatecheck if i use database.stateful
v varaprasadv varaprasad
Hi Pradeep,

To avoid error check once below link : 
https://help.salesforce.com/articleView?id=000239885&type=1

Thanks
Varaprasad
This was selected as the best answer