function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Singh 116Deepak Singh 116 

First error: List index out of bounds: 34

Hi ,
I have written the following batch which update the description from o field CurrentGenerators__c both the fields are on opportunity but when i execute this batch i get the error First error: List index out of bounds: 34.
Please help.

global with sharing class errorinBatch implements Database.Batchable<SObject>,Database.stateful {
global set <String> log;
global database.QueryLocator start(Database.BatchableContext bc){
string query='select id,name,Description,CurrentGenerators__c from opportunity';
system.debug(database.getQueryLocator(query));
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<SObject> scope){
System.debug(scope);
System.debug('====>>>>>'+scope.size());
//List<Opportunity> opp=new List<Opportunity>();
if(scope.size()>0){
for(Opportunity op:(List<Opportunity>)scope){
System.debug(op.Name);
op.Description=op.CurrentGenerators__c;
// opp.add(op);
}
}
database.SaveResult[] result=database.update(scope,false);
System.debug('====>Result'+result);
Integer rsize=result.size();
for(integer i=0;i<=rsize;i++){
System.debug('for loop Block');
System.debug('====>Result[0]'+result[0]);
System.debug('=====>'+i);
System.debug('+++++>>'+scope[i].Id);
if(!result[i].isSuccess()){
log=new Set<String>();
log.add('Error in Opportunity'+scope[i].Id+'Error Message'+result[i].getErrors()[0].getMessage());
System.debug(log);
}
else {
system.debug('+++++>>> updated successfully');
}
}
}
global void Finish(Database.BatchableContext bc){
//System.debug(log);
}
}
 
Best Answer chosen by Deepak Singh 116
Ajay K DubediAjay K Dubedi
Hi Deepak,
You just remove '=' Singh from this line:
for(integer i = 0 ; i <= rsize ; i++)
that will become
for(integer i = 0 ; i < rsize ; i++)
Try this code:
global with sharing class errorinBatch implements Database.Batchable<SObject>,Database.stateful {
    global set <String> log;
    global database.QueryLocator start(Database.BatchableContext bc) {
        string query = 'select Id, Name, Description, CurrentGenerators__c from Opportunity';
        system.debug('query---------->' + database.getQueryLocator(query));
        return database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Opportunity> scope) {
        System.debug('scope------->' + scope);
        System.debug('scope.size()====>>>>>'+scope.size());
        if(scope.size() > 0) {
            for(Opportunity op : scope){
                System.debug(op.Name);
                op.Description = op.CurrentGenerators__c;
            }
        }
        
        database.SaveResult[] result = database.update(scope, false);
        System.debug('Result------->' + result);
        Integer rsize = result.size();
        for(integer i = 0 ; i < rsize ; i++) {
            if(!result[i].isSuccess()){
                log = new Set<String>();
                log.add('Error in Opportunity'+scope[i].Id + 'Error Message' + result[i].getErrors()[0].getMessage());
                System.debug(log);
            }
            else {
                system.debug('updated successfully');
            }
        }
    }
    global void Finish(Database.BatchableContext bc){
        //System.debug(log);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Deepak,
You just remove '=' Singh from this line:
for(integer i = 0 ; i <= rsize ; i++)
that will become
for(integer i = 0 ; i < rsize ; i++)
Try this code:
global with sharing class errorinBatch implements Database.Batchable<SObject>,Database.stateful {
    global set <String> log;
    global database.QueryLocator start(Database.BatchableContext bc) {
        string query = 'select Id, Name, Description, CurrentGenerators__c from Opportunity';
        system.debug('query---------->' + database.getQueryLocator(query));
        return database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Opportunity> scope) {
        System.debug('scope------->' + scope);
        System.debug('scope.size()====>>>>>'+scope.size());
        if(scope.size() > 0) {
            for(Opportunity op : scope){
                System.debug(op.Name);
                op.Description = op.CurrentGenerators__c;
            }
        }
        
        database.SaveResult[] result = database.update(scope, false);
        System.debug('Result------->' + result);
        Integer rsize = result.size();
        for(integer i = 0 ; i < rsize ; i++) {
            if(!result[i].isSuccess()){
                log = new Set<String>();
                log.add('Error in Opportunity'+scope[i].Id + 'Error Message' + result[i].getErrors()[0].getMessage());
                System.debug(log);
            }
            else {
                system.debug('updated successfully');
            }
        }
    }
    global void Finish(Database.BatchableContext bc){
        //System.debug(log);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Deepak Singh 116Deepak Singh 116
Thanks Aajay.
It works.