My class is

List<CampaignMember> cm;  List<Campaign> camp1;  
// if suppose transactions.size() is 2.
for(integer t=0;t<transactions.size();t++){      
         if(transactions[t].Campaign__c != '' || transactions[t].Campaign__c != null){
                   camp1 = chk.Campaigninsert(transactions[t].Campaign__c);                  
                      transactions[t].sf_campaign__c = camp1[t].Id; //Getting error here
                     system.debug('zzzzz'+ transactions[t].sf_campaign__c);

camp1[t].id here for the first Time I loop it is taking the second record also.Don't know why.If I replace it with camp1[0].id it works fine.But always the first Record is passed.How should I restrict it?

  • Potentially camp1 has only one value in it, which is why camp1[0] works, but camp[1] gives IndexOutOfBounds. You're using the same loop variable 't' for inner and outer loops. Mar 28 '13 at 10:57
  • @techtrekker for the first time it enter into loop t contains 0,how can I get exception for t =1 ?
    – Eagerin Sf
    Mar 28 '13 at 11:49

You are using the same loop variable "t" for referencing the elements of both "transactions" and "camp1". Also you are using the size of "tranactions" to iterate over. However, it may not always be true that both the lists have the same number of elements.

For eg: "transactions" may have 2 elements and "camp1" may have 1 element. in that case, you will get the error that you are currently facing.

So when for the first time you refer camp1[0], it works because there is 1 element. However, when you say camp1[1] it is going to throw an error, since there is nothing at index 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.