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
Sanjana RajasekarSanjana Rajasekar 

Batch Class - Help

Hi , I am using a batch class. I need to get the size of the records processed in a variable after all batches in finish method.  I dont want to use Database.stateful because I am hitting Batchable instance is too Big.
https://help.salesforce.com/s/articleView?id=000383707&type=1
Is there any other way we can Achieve it?

Thanks 
Prateek Prasoon 25Prateek Prasoon 25
public class MyBatch implements Database.Batchable<SObject> {
    public static Integer recordCount = 0;
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([your Query]);
    }
    public void execute(Database.BatchableContext bc, List<SObject> scope) {
       recordCount += scope.size();
    }
    public void finish(Database.BatchableContext bc) {
      System.debug('Total records processed: ' + recordCount);
    }
}

If you find this answer helpful,Please mark it as the best answer.