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
Suchawalee Jentrakulroj 9Suchawalee Jentrakulroj 9 

Display all Files (Pictures) from Attachment in Visualforce 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>
Best Answer chosen by Suchawalee Jentrakulroj 9
Lokesh__PareekLokesh__Pareek
Hi Suchawalee,
What you are doing is trying to access 0 index of an empty List which is giving you IndexOutOfBoundException.
Change the line,
fileId[i] = attachedFiles[i].Id;
with
fieldId.add(attachedFiles[i].Id);

Example :
 
List<String> li = new List<String>();
System.debug(li.isEmpty());     // true


Also on page you must iterate the list,
 
<apex:page standardController="Account" extensions="displayImageExtension">
<apex:form >
    <apex:repeat value="{!FileId}" var="v">
        <apex:image url="/servlet/servlet.FileDownload?file={!v}"/>
    </apex:repeat> 
</apex:form>
</apex:page>

Hope it helps!
 

All Answers

Lokesh__PareekLokesh__Pareek
Hi Suchawalee,
What you are doing is trying to access 0 index of an empty List which is giving you IndexOutOfBoundException.
Change the line,
fileId[i] = attachedFiles[i].Id;
with
fieldId.add(attachedFiles[i].Id);

Example :
 
List<String> li = new List<String>();
System.debug(li.isEmpty());     // true


Also on page you must iterate the list,
 
<apex:page standardController="Account" extensions="displayImageExtension">
<apex:form >
    <apex:repeat value="{!FileId}" var="v">
        <apex:image url="/servlet/servlet.FileDownload?file={!v}"/>
    </apex:repeat> 
</apex:form>
</apex:page>

Hope it helps!
 
This was selected as the best answer
Suchawalee Jentrakulroj 9Suchawalee Jentrakulroj 9
It works! Thank you. :D