Monday, March 13, 2023

Iterable Batch Apex Salesforce

        Iterable Batch Example

If your logic is based on rows from an SObject then the Database.QueryLocator is the way to go as that allows the batchable to query very large numbers of records.

But sometimes you may want to do some work that isn't directly related to SObject rows. In that case the iterable object can simply be an array/list:

public class ExampleBatchApex implements Database.Batchable<String>{
public Iterable<String> start(Database.BatchableContext BC){
    return new List<String> { 'When', 'shall this' , 'quarantine be over' };
}
public void execute(Database.BatchableContext info, List<String> strList){
    //let’s do something with the string for fun
    String myStr = strList[0];
}
public void finish(Database.BatchableContext info) { }
}

// executing the batch apex
Id jobId = Database.executeBatch(new ExampleBatchApex());

No comments:

Post a Comment