Iterable interface Batch Apex/Batch Apex Examples

Here we are showing example of Iterable Interface in batch apex


global class BatchIterableMethods implements Database.Batchable<Account>, Iterable<Account>, Iterator<Account> {
  public integer i;
  List<Account> acc;
  public BatchIterableMethods() {
    acc = [select Id from Account];
    i = 0;
  }
  public Iterator<Account> iterator() {
    return this;
  }
  public boolean hasNext() {
    if(acc.size() <= i) 
      return false;
    else
      return true;
  }
  public Account next() {
    i++;
    return acc[i-1];
  }
  global Iterable<Account> start(Database.BatchableContext bc) {
    return new BatchIterableMethods();
  } 
  global void execute(Database.BatchableContext bc, List<Account> scope) {
    for(Account a : scope) {
      a.AccountSource = 'Other';
    }
    update scope;
  }
  global void finish(Database.BatchableContext bc) {}
}

Batch Apex Examples


The following example uses a Database.QueryLocator:

public class UpdateAccountFields implements Database.Batchable<sObject>{
 public final String Query;
 public final String Entity;
 public final String Field;
 public final String Value;
 
 public UpdateAccountFields(String q, String e, String f, String v){
 Query=q; Entity=e; Field=f;Value=v;
 }
 
 public Database.QueryLocator start(Database.BatchableContext BC){
 return Database.getQueryLocator(query);
 }
 
 public void execute(Database.BatchableContext BC, 
 List<sObject> scope){
 for(Sobject s : scope){s.put(Field,Value); 
 }      update scope;
 }
 
 public void finish(Database.BatchableContext BC){
 
 }
 
}

You can use the following code to call the previous class.

String q = 'SELECT Industry FROM Account LIMIT 10';
String e = 'Account';
String f = 'Industry';
String v = 'Consulting';
Id batchInstanceId = Database.executeBatch(new UpdateAccountFields(q,e,f,v), 5); 

Using Callouts in Batch Apex

To use a callout in batch Apex, specify Database.AllowsCallouts in the class definition. For example:

public class SearchAndReplace implements Database.Batchable<sObject>, 
 Database.AllowsCallouts{
}

Using State in Batch Apex

If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions. Maintaining state is useful for counting or summarizing records as they’re processed.

If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.

The following example summarizes a custom field total__c as the records are processed.

public class SummarizeAccountTotal implements 
 Database.Batchable<sObject>, Database.Stateful{
 
 public final String Query;
 public integer Summary;
 
 public SummarizeAccountTotal(String q){Query=q;
 Summary = 0;
 }
 
 public Database.QueryLocator start(Database.BatchableContext BC){
 return Database.getQueryLocator(query);
 }
 
 public void execute(
 Database.BatchableContext BC, 
 List<sObject> scope){
 for(sObject s : scope){
 Summary = Integer.valueOf(s.get('total__c'))+Summary;
 }
 }
 
public void finish(Database.BatchableContext BC){
 }
}

In addition, you can specify a variable to access the initial state of the class. You can use this variable to share the initial state with all instances of the Database.Batchable methods. For example:

// Implement the interface using a list of Account sObjects
// Note that the initialState variable is declared as final
 
public class MyBatchable implements Database.Batchable<sObject> {
 private final String initialState;
 String query;
 
 public MyBatchable(String intialState) {
 this.initialState = initialState;
 }
 
 public Database.QueryLocator start(Database.BatchableContext BC) {
 // Access initialState here 
 
 return Database.getQueryLocator(query);
 }
 
 public void execute(Database.BatchableContext BC, 
 List<sObject> batch) {
 // Access initialState here 
 
 }
 
 public void finish(Database.BatchableContext BC) {
 // Access initialState here 
 
 }
}

Batch Apex Governor Limits

  • Up to 5 batch jobs can be queued or active concurrently.

  • 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.

  • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200 records. The system then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.

  • The start, execute, and finish methods can implement up to 100 callouts each.

  • The maximum number of batch Apex method executions per 24-hour period is 250,000, or the number of user licenses in your org multiplied by 200—whichever is greater. Method executions include executions of the start, execute, and finish methods.

  • Batch apex can process a maximum of 50Million records in case of Database.QueryLocator return type and 50K records in case of Iterable return type.




5 views0 comments

Recent Posts

See All

Salesforce Configuration Topics

CRM - Customer Relationship Management 1.             What is CRM(Customer Relationship Management)? 2.             CRM Basic entities 3.             Sub Modules of CRM 4.             Existing CRMs Sa

Daily Information | Daliylinx are related to General information, Technology, Salesforce, Newspapers, Telugu Newspapers, Pets, Cars etc

  • Facebook