You can add a property to the controller that returns the collection of attachments using the "lazy load" pattern like this:
public Attachment[] attachments {
get {
if (attachments == null) {
attachments = [
select Id, Name, Description, LastModifiedDate
from Attachment
where ParentId = :customObjectId
order by Name
];
}
return attachments;
}
private set;
}
and use e.g. an apex:pageBlockTable
to display the attachment fields as apex:column
s.
If you want to have a "View" link in your table that opens the attachment in another tab/window, and don't mind relying on a URL that Salesforce may change in the future, this will work (assuming item
is an Attachment):
<apex:outputLink
styleClass="actionLink"
target="_BLANK"
value="/servlet/servlet.FileDownload?file={!item.Id}"
>View</apex:outputLink>
Do not query the Body
of the attachments as you cannot display that (in general) and you will run the risk of exceeding the allowed heap space in your controller.