You need to sign in to do that
Don't have an account?
Suchawalee 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>
"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>
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 :
Also on page you must iterate the list,
Hope it helps!
All Answers
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 :
Also on page you must iterate the list,
Hope it helps!