1

How can I set a limit on the number of records processed in my batch process?
Say the QueryLocator pulls 500 records. I'd like to break this up so 500 records are processed 10 records at a time.

CleanUpRecords.cls

global class CleanUpRecords implements Database.Batchable<sObject>{
    
    global final String query;
        
    global CleanUpRecords (String q) {
        query = q;
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        delete scope;
        Database.emptyRecycleBin(scope);
    }
    
    
    global void finish(Database.BatchableContext BC){
        AsyncApexJob a = 
            [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
            TotalJobItems, CreatedBy.Email 
            FROM AsyncApexJob WHERE Id = 
            :BC.getJobId()];
            
    }
    
}
0

1 Answer 1

13

You can specify the batch size when calling the batch via:

Database.executeBatch(Object batchClassObject, Integer scope).

The scope integer is how many records will be passed to your execute() method. Some other notes from the documentation:

The value for scope must be greater than 0.

If the start method of the batch class returns a Database.QueryLocator, the scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 200 records. If the start method of the batch class returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you could run into other limits.

Apex governor limits are reset for each execution of execute.

Schedule your class either in code, or through the developer console, with this snippet:

CleanUpRecords batch = new CleanUpRecords();  
Database.executeBatch(batch, 10);
9
  • From the documentation: This parameter specifies the number of records that should be passed into the execute method. Use this parameter when you have many operations for each record being passed in and are running into governor limits. This value must be greater than zero. If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. Commented Jan 9, 2015 at 19:28
  • Documenation: salesforce.com/us/developer/docs/apexcode/Content/… Commented Jan 9, 2015 at 19:29
  • I'm not quite getting how to place this parameter in my class, even after looking at the documentation. How would I modify this section of my code? Commented Jan 9, 2015 at 20:29
  • global void execute(Database.BatchableContext BC, List<sObject> scope){ delete scope; Database.emptyRecycleBin(scope); } Commented Jan 9, 2015 at 20:29
  • So what would help me is if I could modify my original code, my original post, so it could set the batch at 10. Commented Jan 9, 2015 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .