Skip to main content
Skip table of contents

Batch Classes

Batch classes are used whenever there is a need to perform a big amount of tasks that may exceed/hit the governor limit of Salesforce. Batch classes make it possible to perform tasks asynchronously and creatively bypass the governor limit by grouping large amount of tasks into a smaller group of tasks (a batch).

To make a batch class, you have to implement it first in your class:

  • Implement the IPlannerBatchV2 interface

  • Implement the 3 required methods (start, execute and finish method).

For example: 

CODE
// Batch class to update multiple Accounts based on their Name
global with sharing class UpdateAccountNameBatch implements sfy24.IPlannerBatchV2, Database.Batchable<SObject> {
    private String dataToUpdate = 'Default Name';
    private String fieldToUpdate = 'Name';
    private String fieldValue = 'Updated Default Name';
    
    // Empty constructor is required AND must be global
    global void UpdateAccountNameBatch() {
    }
    
    global void setData(DataSource__c dataSource, List<Event> events) {
    }
    
    // start method - used to specify the query locator for the batch job
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id, Name FROM Account WHERE Name = :dataToUpdate]);
    }
    
    // execute method - used to process each batch of records
    public void execute(Database.BatchableContext bc, List<Account> scope) {
        for (Account acc : scope) {
            acc.put(fieldToUpdate, fieldValue);
        }
        update scope;
    }
    
    // finish method - used for any post-processing tasks after all batches are processed
    public void finish(Database.BatchableContext bc) {
        System.debug('Batch update completed.');
    }
}

Once integrated in 24Planning (permission set might be needed), you should be able to select this Batch class in the datasource configuration (see the Datasource configuration section of this guide) and execute it according to the selected Run Batch class options.

Useful information regarding Batch classes
Executing/invoking batch classes: start with making an instance of it, then you can execute it with Database.executeBatch(instanceOfBatchObj)

CODE
// execute/invocate a Catch class, an example:

BatchUpdate bUpdate = new BatchUpdate();

ID batchprocessid = Database.executeBatch(bUpdate);

System.debug('Returned batch process ID: ' + batchProcessId);

The function can also take 2nd parameter, an integer, to limit the amount of records that should be passed for each batch. It is a recommend practice to make use of the 2nd parameter to avoid hitting the governor limit.

Each batch invocation also creates a record called AsyncApexJob to keep track of its progress. You can view this progress though SOQL or Apex Job Queue, the latter can be accessed through [Setup > Search > ‘Apex Jobs’] in Salesforce.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.