+ Start a Discussion
tulasiram chtulasiram ch 

I used following batch apex for inserting 20000 records but i am gettong null exception pls suggest me


global class batchForinsertingAccounts implements Database.Batchable<sObject>, Database.Stateful{
global integer count;
global string query;
global Database.QueryLocator start(Database.BatchableContext BC){
      count=0;
      return null;
}
global void execute(Database.BatchableContext BC, List<account> scope){
count=0;
list<Account> Llist = new list<Account>();
for(account acc:scope){
for(integer i=0;i<=20000 ;i++){
Account a = new account();
a.name = 'TulasiRam'+i;
Llist.add(acc);
count++;
}
}
database.insert(Llist);

}
global void finish(Database.BatchableContext BC){

system.debug(''+count);
}
}
Best Answer chosen by tulasiram ch
Raj VakatiRaj Vakati
Hi , 
You can't insert new records in the batch apex.Batch Apex is to manipulate the data.Use Normal Utility class or Queueable 
 
public class batchForinsertingAccounts implements Queueable {
public void execute(QueueableContext context) {
count=0;
list<Account> Llist = new list<Account>();
 for(integer i=0;i<=20000 ;i++){
Account a = new account();
a.name = 'TulasiRam'+i;
Llist.add(acc);
count++;
}
 
database.insert(Llist);    
}
}

 

All Answers

Raj VakatiRaj Vakati
Hi , 
You can't insert new records in the batch apex.Batch Apex is to manipulate the data.Use Normal Utility class or Queueable 
 
public class batchForinsertingAccounts implements Queueable {
public void execute(QueueableContext context) {
count=0;
list<Account> Llist = new list<Account>();
 for(integer i=0;i<=20000 ;i++){
Account a = new account();
a.name = 'TulasiRam'+i;
Llist.add(acc);
count++;
}
 
database.insert(Llist);    
}
}

 
This was selected as the best answer
Alain CabonAlain Cabon
Hi,

for(integer i=0;i<=20000 ;i++){
      Account a = new account();
      a.name = 'TulasiRam'+i;
      Llist.add(a);
      Llist.add(acc);
      count++;
}

Minimal test:

Id batchInstanceId = Database.executeBatch(new batchForinsertingAccounts());
system.debug('instance id:' + batchInstanceId);
global class batchForinsertingAccounts implements Database.Batchable<sObject>, Database.Stateful{
    global integer count;
    global string query;
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('select id from account limit 1');
    }
    
    global void execute(Database.BatchableContext BC, List<account> scope){
        count=0;
        list<Account> Llist = new list<Account>();
        for(account acc:scope){
            for(integer i=0;i<=2 ;i++){
                Account a = new account();
                a.name = 'TulasiRam'+i;
                Llist.add(a);
                count++;
            }
        }
        
        Database.SaveResult[] srList = Database.insert(Llist, false);
        
        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted account. Account ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Fields that affected this error: ' + err.getFields());
                }
            } 
        }   
    }
    global void finish(Database.BatchableContext BC){        
        system.debug('final count: '+ count);
    }
}

Regards
Alain
tulasiram chtulasiram ch
Hi Alain cobon, is it work for inserting 20000 records at a time. It's not working