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
Salvatore GomezSalvatore Gomez 

List index out of bounds: 1

Fair warning i'm an Apex Noob. I'm getting a List index out of bounds: 1 error on the code below. It seems like the issue is with the Att list since the query may or may not return an attachment for each account. Of course i could be wrong on that any suggestions on how to rework this would be greatly appreciated.

Apex code:
Public class PhotoWrapper{
List<Account> Acc = new List<Account>();
List<Opportunity> Opp = new List<Opportunity>();
List<wrapper> lstw = new List<wrapper>();
List<Attachment> Att = new List<Attachment>();
Public List<wrapper> getLstWrapperString()
{
    Acc=[SELECT Id,Name,PersonEmail,(Select Id, ParentId from Attachments where Name='Contact Picture') FROM Account limit 20];
    Att=[Select Id, ParentId from Attachment where Name='Contact Picture' and ParentId in :Acc];
    Opp=[SELECT PLAN_DESCR__c,ADMIT_TERM_DESCR__c,JOB_TITLE__c, AccountId FROM Opportunity where AccountId in : Acc];
    for(integer i=0;i<Acc.size();i++)
    {
        lstw.add(new wrapper(Acc[i].Id,Acc[i].name, Acc[i].PersonEmail,Opp[i].PLAN_DESCR__c,Opp[i].ADMIT_TERM_DESCR__c,Opp[i].JOB_TITLE__c,Opp[i].AccountId,Att[i].Id));
   }
    return lstw;
}

Public class wrapper{
    Public String Aname {get;set;}
    Public String Aemail {get;set;}
    Public String OPLAN_DESCR {get;set;}
    Public String OADMIT_TERM {get;set;}
    Public String OJOB_TITLE {get;set;}
    Public String AccId {get;set;}
    Public String OAccId {get;set;}
    Public String AttId {get;set;}

Public wrapper(String AccId,String Aname, String Aemail,String OPLAN_DESCR, String OADMIT_TERM,String OJOB_TITLE, String OAccId,String AttId){
     this.Aname=Aname;
     this.Aemail=Aemail;
     this.OPLAN_DESCR=OPLAN_DESCR;
     this.OADMIT_TERM = OADMIT_TERM;
     this.OJOB_TITLE = OJOB_TITLE;
     AccId=OAccId;
     this.AttId=AttId;
 }

}
}
Shalom RubdiShalom Rubdi
Hi Salvatore - the issue might be with your for loop, you are starting it correctly with the zero-based index, but need to stop iterating when you get to the end of the list which is Acc.size-1; therefore, try:

for(integer i=0;i<Acc.size-1();i++)
Salvatore GomezSalvatore Gomez
Thank you for the suggestion Shalom.
When I changed the for loop to  as you suggested I get the following Error: Compile Error: expecting a semi-colon, found '(' at line 20 column 32

I went ahead and modified your code by placing the -1 behind the parenthesis like so

for(integer i=0;i<Acc.size()-1;i++)

and the code executed but still recieved the List index out of bounds: 1
 
Naveen IlaNaveen Ila
Hi Salvatore Gomez,

The code you written above correct in syntatical wise.

In logical you might wrong. 


Check below code. 
Instead of firing 3 quries. you may get the all the data in singe query:

 Acc=[SELECT Id,Name,PersonEmail,(Select Id, ParentId from Attachments where Name='Contact Picture') FROM Account limit 20];
    Att=[Select Id, ParentId from Attachment where Name='Contact Picture' and ParentId in :Acc];
    Opp=[SELECT PLAN_DESCR__c,ADMIT_TERM_DESCR__c,JOB_TITLE__c, AccountId FROM Opportunity where AccountId in : Acc];

Replace above code with:

Acc = [
                     SELECT Id,Name,PersonEmail
                    ,(Select Id, ParentId from Attachments where Name='Contact Picture')
                    ,( SELECT PLAN_DESCR__c,ADMIT_TERM_DESCR__c,JOB_TITLE__c, AccountId FROM Opportunities)
              FROM Account
        limit 20
     ];

=====================================================================================================

Below code not so clear. 

for(integer i=0;i<Acc.size();i++)
    {
        lstw.add(new wrapper(Acc[i].Id,Acc[i].name, Acc[i].PersonEmail,Opp[i].PLAN_DESCR__c,Opp[i].ADMIT_TERM_DESCR__c,Opp[i].JOB_TITLE__c,Opp[i].AccountId,Att[i].Id));
   }
    return lstw;

Can you please provide me the scenario for the for loop.  

 There is 1 to many relationship in Between Account and Opportunity

Take the above above senario, suppose you have 100 accounts in your org. Since there is no where condition on Account you are able to fetch 20.

If you loop through these accouts. Condier the situation where are at 18 .
At this moment if are in the loop:
In order to create a object to wraper we need to have:
1) Atleat 18 opportunities which are might or might belogs to Acc[18]
2) Atleat 18 Attachments with Name='Contact Picture'  might or might belogs to Acc[18]. 

Since Opp[i].PLAN_DESCR__c refers to opp[18].PLAN_DESCR__c

I think you need to remodify above for loop. if so please let me the senario you want to implement. 









 
Salvatore GomezSalvatore Gomez
Hello Naveen,

Thanks for your reply and suggestion. Originally I tried using a single query similar to your suggestion and i encountered some issues with outputting the result set of that query to a datatable. I read on another forum that suggested using a wrapper class in this scenario to overcome the challenges with the multiple data types and outputting the results to a data table. Since the wrapper class needs the result set of each query to have 18 records then the wrapper class will not work for me.

So here's the scenario i'm trying to return a contact picture from the Account based on the type of opportunity. The single query you suggested would be perfect in this situation if i could figure out how to output the result set to a data table. There is a good chance that the contact picture isn't attached yet so this would explain why the wrapper class solution i attempted didn't work.

If you have any suggestion on how to make this work please let me know, thanks.