• Suchawalee Jentrakulroj 9
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi, I'm trying to have all pictures and pdfs in the attachment of a record displayed in a Visualforce page when a user klick on a button"View all files". I keep getting the Error:
"System.ListException: List index out of bounds: 0
Class.displayImageExtension.getFileId: line 15, column 1"

I'm an administrator and just beginning to code. Please help. Thank you.

here is my Code:
Controller:
Public Class displayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public List<String> getFileId() {
        String[] fileId = new List<String>();
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC ];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
        for (Integer i=0 ; i < attachedFiles.size(); i++) {   
            
            fileId[i] = attachedFiles[i].Id;
            }      
        }
        return fileId;
            
    }
}

Visualforce page:
<apex:page standardController="Account" extensions="displayImageExtension">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>
Hi, I'm trying to have all pictures and pdfs in the attachment of a record displayed in a Visualforce page when a user klick on a button"View all files". I keep getting the Error:
"System.ListException: List index out of bounds: 0
Class.displayImageExtension.getFileId: line 15, column 1"

I'm an administrator and just beginning to code. Please help. Thank you.

here is my Code:
Controller:
Public Class displayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public List<String> getFileId() {
        String[] fileId = new List<String>();
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC ];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
        for (Integer i=0 ; i < attachedFiles.size(); i++) {   
            
            fileId[i] = attachedFiles[i].Id;
            }      
        }
        return fileId;
            
    }
}

Visualforce page:
<apex:page standardController="Account" extensions="displayImageExtension">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>