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
benton22cbenton22c 

Viewing Attachments by child object

Newbie!!

 

I'm trying to create a VF Page that displays all of a Parent's child objects attributes along with their associated attachments. My controller passes all child objects attachments into a list but my VF page displays all the attachments of every child within each childs list. We want a vf page where each child's name and accompany photo attachments are listed within their own section.

 

Controller:

// Throw all attachments into myPhotos list

private void fetchPhotos ()
{
myPhotos.clear (); // Empty list

for (Line_Item__c b : myChildlist) {
for (Attachment a : [Select Id, Name, ParentId, ContentType From Attachment Where ParentId = :b.Id])
{
myPhotos.add (new Photo (a.Id, a.Name, a.ParentID));
}
}
}

 

VF Page:

// Use nested repeat list to display attachments

<apex:repeat value="{!myChildlist}" var="it">
<apex:repeat value="{!Photos}" var="p">
<img src="{!p.url}" width="296px" height="225px"/>
</apex:repeat>
</apex:repeat>

 

All attachments from every line_item__c child object are displayed within every line_item__c repeat

 

bob_buzzardbob_buzzard

This is because you are adding all the attachments to a single list - you'll need to use a wrapper class for this, which encapsulates a line item and its attachments.  Then you would iterate through a list of the wrapper classes, and the nested repeat would iterate its list of attachments.