You need to sign in to do that
Don't have an account?
Dipak@pvs
Reg: System.ListException: List index out of bounds: 2
Hi All,
I am getting System.ListException: List index out of bounds: 2 error while running a test class. My class and Test class are:
Class:
global class ArpDataCleanupBatch implements Database.Batchable<SObject>, Database.Stateful{
private List<Id> AlmRepPerfIds;
private String MarketCleanupId;
//private List<Id> CalIds;
public ArpDataCleanupBatch() {
//do nothing
}
public ArpDataCleanupBatch(List<Id> AlmRepPerfIds) {
this.AlmRepPerfIds = AlmRepPerfIds;
}
public ArpDataCleanupBatch(String MarketId) {
this.MarketCleanupId = MarketId;
}
public ArpDataCleanupBatch(String MarketId, List<Id> AlmRepPerfIds) {
this.MarketCleanupId = MarketId;
this.AlmRepPerfIds = AlmRepPerfIds;
// this.CalIds = CalIds;
}
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator(prepareSOQL());
}
global void execute(Database.BatchableContext bc, List<ALM_Rep_Performance_1__c> rows){
delete rows;
// DataBase.emptyRecycleBin(rows);
}
global void finish(Database.BatchableContext bc){
String CleanupLastRun;
datetime myDateTime = datetime.now();
String myDateStr = String.ValueOf(myDateTime);
Market__c mkt = [select Id,Cleanup_Last_Run__c from market__c
where Market_Id__c=:MarketCleanupId];
mkt.Cleanup_Last_Run__c = 'ALM_Rep_Performance_1__c:'+myDateStr;
update mkt;
Database.executeBatch(new SlhDataCleanupBatch('73'),2000);
System.debug('job finished.');
}
public static void startJob() {
scheduleJob(2);
}
private String prepareSOQL() {
Market__c mrkt = [select PREV_1_CMPGN_NR__c,X90_Day_Camp_CNT__c from market__c
where Market_Id__c=:MarketCleanupId];
List<Calendar__c> camp = [select CMPGN_NR__c from Calendar__c where MRKT_ID__c = :MarketCleanupId order by CMPGN_NR__c desc];
Integer index = -1;
Integer CampaignIndex=1;
List<String> Camps=new List<String>();
for(Calendar__c ca: camp){
Camps.add(String.valueOf(ca.CMPGN_NR__c));
}
for(Integer m = 0; m<Camps.size();m++){
IF(Camps[m].equalsignorecase(mrkt.PREV_1_CMPGN_NR__c)){
index = m;
System.debug('index='+ index);
break;
}
}
String PrevCmpgnStr=Camps[index+CampaignIndex];
Decimal myDecimal = decimal.valueOf(PrevCmpgnStr);
system.debug('prevcamps'+ PrevCmpgnStr);
// For testing
Set<Id> ptIds = new Set<Id>{'a1KZ0000001o81u','a1KZ0000001o81z','a1KZ0000001o820'};
List<Id> llistId = New List<Id>();
llistId.addAll(ptIds);
String idStrings= '\''+String.join(llistId,'\',\'')+'\'';
String sql = 'SELECT Id FROM ALM_Rep_Performance_1__c ';
sql += ' where Id In ('+idStrings+') ' ;
// End For testing
/* String sql = 'SELECT FLD_SLS_CMPGN_NR__c, id FROM ALM_Rep_Performance_1__c ';
sql += ' where FLD_SLS_CMPGN_NR__c = '+myDecimal+' and MRKT_ID__c =\''+MarketCleanupId+'\'';*/
system.debug('query'+ sql);
return sql;
}
private Set<String> fetchEligibleMarketCodes() {
Set<String> marketCodes = new Set<String>();
for (Market__c market : [Select Market_Id__c from Market__c where Is_ALMPLUS_Enabled__c=true]) {
marketCodes.add(market.Market_Id__c);
}
System.debug('eligible market codes: ' + marketCodes);
return marketCodes;
}
private static void scheduleJob(Integer minutesOffset) {
DateTime currentTime = System.now();
String scheduleTime = ALMPlusUtils.prepareJobsSchTime(currentTime, minutesOffset);
String uniqueJobName = ALMPlusUtils.getUniqueJobName('ArpDataCleanupBatch');
String cronID = System.schedule(uniqueJobName, scheduleTime, new BatchJobScheduler(new ArpDataCleanupBatch(), 1000)); //use 10 as the batch size
}
//ArpDataCleanupBatch arp=new ArpDataCleanupBatch('73');
//arp.startJob();
}
Test Class:
@isTest
private class ArpDataCleanupBatchTest {
static testMethod void testBatchJob() {
User loginUser = new User(Id=UserInfo.getUserId());
TestDataHelper td = new TestDataHelper();
Market__c mrkt = TestDataHelper.createMarket('test market', '1234', false);
mrkt.ALM_Rep_Indicator__c = 'ALM_Rep_Performance_1__c';
mrkt.PREV_1_CMPGN_NR__c = '201111';
mrkt.X90_Day_Camp_CNT__c = 4;
mrkt.Mail_Plan_Enabled__c = true;
mrkt.CMPGN_NR__c = '1234567';
mrkt.Is_Cleanup_Enabled__c= true;
insert mrkt;
System.runAs(loginUser) {
Account acc = TestDataHelper.createAccount('test', 'USD', true);
Contact con = TestDataHelper.createContact('1234', acc.Id, true);
List<Id> AlmRepPerfIds = new List<Id>();
String MarketCleanupId;
MarketCleanupId = mrkt.Market_Id__c;
List<ALM_Rep_Performance_1__c> arp = new List<ALM_Rep_Performance_1__c>();
ALM_Rep_Performance_1__c arp1 = new ALM_Rep_Performance_1__c(Name = 'Test Name',FLD_SLS_CMPGN_NR__c = 201312,MRKT_ID__c = '73',
Personal_Performance_Key__c = '1233444322',MRKT_ACCT_KEY__c = con.Id,MRKT_KEY__c =mrkt.Id);
insert arp1;
arp.add(arp1);
List<Calendar__c> cal = new List<Calendar__c>();
Calendar__c cal1 = new Calendar__c(CMPGN_NR__c = 201312, MRKT_ID__c = '73', YEAR_NR__c = 4,
CMPGN_STRT_DT__c = date.valueOf('2013-04-02'), PRIOR_CMPGN_NR__c='201311', Market__c =mrkt.Id);
Calendar__c cal2 = new Calendar__c(CMPGN_NR__c = 201312, MRKT_ID__c = '73', YEAR_NR__c = 4,
CMPGN_STRT_DT__c = date.valueOf('2013-05-02'), PRIOR_CMPGN_NR__c='201311',Market__c =mrkt.Id);
cal.add(cal1);
cal.add(cal2);
insert cal;
System.debug('cal1 ->>'+cal1);
System.debug('cal2 ->>'+cal2);
Set<Id> CalIds = new Set<Id>();
CalIds.add(cal1.Id);
CalIds.add(cal2.Id);
Integer index = -1;
Integer CampaignIndex=1;
List<String> Camps=new List<String>();
system.debug('string is'+ cal);
for(Calendar__c ca: cal){
Camps.add(String.valueOf(ca.CMPGN_NR__c));
}
for(Integer m = 0; m<Camps.size();m++){
index = m;
System.debug('index='+ index);
}
String PrevCmpgnStr=Camps[index+CampaignIndex];
system.debug('string for index'+ PrevCmpgnStr);
Test.startTest();
Database.executeBatch(new ArpDataCleanupBatch(MarketCleanupId,AlmRepPerfIds), 10);
ArpDataCleanupBatch.startJob();
Test.stopTest();
// System.assert(0 == [Select count() from ALM_Rep_Performance_1__c where Id=:arp.Id]);
}
List<ALM_Rep_Performance_1__c> alerts = [select Id,FLD_SLS_CMPGN_NR__c from ALM_Rep_Performance_1__c];
if(alerts.size() > 0) {
System.assert(alerts.size() == 1);
System.assert(alerts[0].FLD_SLS_CMPGN_NR__c == 201312);
}
}
}.
It gives me error like this:
System.ListException: List index out of bounds: 2
Class.ArpDataCleanupBatchTest.testBatchJob: line 67, column 1(line no. 67:--String PrevCmpgnStr=Camps[index+CampaignIndex];)
Any help will be greatly appreciated.
Regards,
Dip
I am getting System.ListException: List index out of bounds: 2 error while running a test class. My class and Test class are:
Class:
global class ArpDataCleanupBatch implements Database.Batchable<SObject>, Database.Stateful{
private List<Id> AlmRepPerfIds;
private String MarketCleanupId;
//private List<Id> CalIds;
public ArpDataCleanupBatch() {
//do nothing
}
public ArpDataCleanupBatch(List<Id> AlmRepPerfIds) {
this.AlmRepPerfIds = AlmRepPerfIds;
}
public ArpDataCleanupBatch(String MarketId) {
this.MarketCleanupId = MarketId;
}
public ArpDataCleanupBatch(String MarketId, List<Id> AlmRepPerfIds) {
this.MarketCleanupId = MarketId;
this.AlmRepPerfIds = AlmRepPerfIds;
// this.CalIds = CalIds;
}
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator(prepareSOQL());
}
global void execute(Database.BatchableContext bc, List<ALM_Rep_Performance_1__c> rows){
delete rows;
// DataBase.emptyRecycleBin(rows);
}
global void finish(Database.BatchableContext bc){
String CleanupLastRun;
datetime myDateTime = datetime.now();
String myDateStr = String.ValueOf(myDateTime);
Market__c mkt = [select Id,Cleanup_Last_Run__c from market__c
where Market_Id__c=:MarketCleanupId];
mkt.Cleanup_Last_Run__c = 'ALM_Rep_Performance_1__c:'+myDateStr;
update mkt;
Database.executeBatch(new SlhDataCleanupBatch('73'),2000);
System.debug('job finished.');
}
public static void startJob() {
scheduleJob(2);
}
private String prepareSOQL() {
Market__c mrkt = [select PREV_1_CMPGN_NR__c,X90_Day_Camp_CNT__c from market__c
where Market_Id__c=:MarketCleanupId];
List<Calendar__c> camp = [select CMPGN_NR__c from Calendar__c where MRKT_ID__c = :MarketCleanupId order by CMPGN_NR__c desc];
Integer index = -1;
Integer CampaignIndex=1;
List<String> Camps=new List<String>();
for(Calendar__c ca: camp){
Camps.add(String.valueOf(ca.CMPGN_NR__c));
}
for(Integer m = 0; m<Camps.size();m++){
IF(Camps[m].equalsignorecase(mrkt.PREV_1_CMPGN_NR__c)){
index = m;
System.debug('index='+ index);
break;
}
}
String PrevCmpgnStr=Camps[index+CampaignIndex];
Decimal myDecimal = decimal.valueOf(PrevCmpgnStr);
system.debug('prevcamps'+ PrevCmpgnStr);
// For testing
Set<Id> ptIds = new Set<Id>{'a1KZ0000001o81u','a1KZ0000001o81z','a1KZ0000001o820'};
List<Id> llistId = New List<Id>();
llistId.addAll(ptIds);
String idStrings= '\''+String.join(llistId,'\',\'')+'\'';
String sql = 'SELECT Id FROM ALM_Rep_Performance_1__c ';
sql += ' where Id In ('+idStrings+') ' ;
// End For testing
/* String sql = 'SELECT FLD_SLS_CMPGN_NR__c, id FROM ALM_Rep_Performance_1__c ';
sql += ' where FLD_SLS_CMPGN_NR__c = '+myDecimal+' and MRKT_ID__c =\''+MarketCleanupId+'\'';*/
system.debug('query'+ sql);
return sql;
}
private Set<String> fetchEligibleMarketCodes() {
Set<String> marketCodes = new Set<String>();
for (Market__c market : [Select Market_Id__c from Market__c where Is_ALMPLUS_Enabled__c=true]) {
marketCodes.add(market.Market_Id__c);
}
System.debug('eligible market codes: ' + marketCodes);
return marketCodes;
}
private static void scheduleJob(Integer minutesOffset) {
DateTime currentTime = System.now();
String scheduleTime = ALMPlusUtils.prepareJobsSchTime(currentTime, minutesOffset);
String uniqueJobName = ALMPlusUtils.getUniqueJobName('ArpDataCleanupBatch');
String cronID = System.schedule(uniqueJobName, scheduleTime, new BatchJobScheduler(new ArpDataCleanupBatch(), 1000)); //use 10 as the batch size
}
//ArpDataCleanupBatch arp=new ArpDataCleanupBatch('73');
//arp.startJob();
}
Test Class:
@isTest
private class ArpDataCleanupBatchTest {
static testMethod void testBatchJob() {
User loginUser = new User(Id=UserInfo.getUserId());
TestDataHelper td = new TestDataHelper();
Market__c mrkt = TestDataHelper.createMarket('test market', '1234', false);
mrkt.ALM_Rep_Indicator__c = 'ALM_Rep_Performance_1__c';
mrkt.PREV_1_CMPGN_NR__c = '201111';
mrkt.X90_Day_Camp_CNT__c = 4;
mrkt.Mail_Plan_Enabled__c = true;
mrkt.CMPGN_NR__c = '1234567';
mrkt.Is_Cleanup_Enabled__c= true;
insert mrkt;
System.runAs(loginUser) {
Account acc = TestDataHelper.createAccount('test', 'USD', true);
Contact con = TestDataHelper.createContact('1234', acc.Id, true);
List<Id> AlmRepPerfIds = new List<Id>();
String MarketCleanupId;
MarketCleanupId = mrkt.Market_Id__c;
List<ALM_Rep_Performance_1__c> arp = new List<ALM_Rep_Performance_1__c>();
ALM_Rep_Performance_1__c arp1 = new ALM_Rep_Performance_1__c(Name = 'Test Name',FLD_SLS_CMPGN_NR__c = 201312,MRKT_ID__c = '73',
Personal_Performance_Key__c = '1233444322',MRKT_ACCT_KEY__c = con.Id,MRKT_KEY__c =mrkt.Id);
insert arp1;
arp.add(arp1);
List<Calendar__c> cal = new List<Calendar__c>();
Calendar__c cal1 = new Calendar__c(CMPGN_NR__c = 201312, MRKT_ID__c = '73', YEAR_NR__c = 4,
CMPGN_STRT_DT__c = date.valueOf('2013-04-02'), PRIOR_CMPGN_NR__c='201311', Market__c =mrkt.Id);
Calendar__c cal2 = new Calendar__c(CMPGN_NR__c = 201312, MRKT_ID__c = '73', YEAR_NR__c = 4,
CMPGN_STRT_DT__c = date.valueOf('2013-05-02'), PRIOR_CMPGN_NR__c='201311',Market__c =mrkt.Id);
cal.add(cal1);
cal.add(cal2);
insert cal;
System.debug('cal1 ->>'+cal1);
System.debug('cal2 ->>'+cal2);
Set<Id> CalIds = new Set<Id>();
CalIds.add(cal1.Id);
CalIds.add(cal2.Id);
Integer index = -1;
Integer CampaignIndex=1;
List<String> Camps=new List<String>();
system.debug('string is'+ cal);
for(Calendar__c ca: cal){
Camps.add(String.valueOf(ca.CMPGN_NR__c));
}
for(Integer m = 0; m<Camps.size();m++){
index = m;
System.debug('index='+ index);
}
String PrevCmpgnStr=Camps[index+CampaignIndex];
system.debug('string for index'+ PrevCmpgnStr);
Test.startTest();
Database.executeBatch(new ArpDataCleanupBatch(MarketCleanupId,AlmRepPerfIds), 10);
ArpDataCleanupBatch.startJob();
Test.stopTest();
// System.assert(0 == [Select count() from ALM_Rep_Performance_1__c where Id=:arp.Id]);
}
List<ALM_Rep_Performance_1__c> alerts = [select Id,FLD_SLS_CMPGN_NR__c from ALM_Rep_Performance_1__c];
if(alerts.size() > 0) {
System.assert(alerts.size() == 1);
System.assert(alerts[0].FLD_SLS_CMPGN_NR__c == 201312);
}
}
}.
It gives me error like this:
System.ListException: List index out of bounds: 2
Class.ArpDataCleanupBatchTest.testBatchJob: line 67, column 1(line no. 67:--String PrevCmpgnStr=Camps[index+CampaignIndex];)
Any help will be greatly appreciated.
Regards,
Dip
If you look at this code in the test class:
Lets say Camps.size() is 10.
So, index=9.
So, index+CampaignIndex=10 (since CampaignIndex=1).
But there is no Camps[10].
Since the size of Camps list is 10, the last element would be Camps[9].
This should be why you are receiving this error.
Thanks,
shashank