1

I am trying to display respective attachment preview on a VF Page, but if you can see below picture of my VF page, same image is getting displayed for all attachments, not sure where i'm going wrongMY VF PAGE DISPLAY

Below is the respective coding part which i have used for this functionality

VF PAGE

<apex:page standardController="Case" extensions="DelAttachments_Img_Controller" >
<apex:form >
<apex:sectionHeader title="Select the Attachments you want to Delete: " description="You can delete more than one attachments from the list below"/>
    <apex:actionFunction name="DoSelection" action="{!selectAttachment}"/>
<apex:pageBlock >
    <apex:pageBlockButtons >
        <apex:commandButton Value="Delete"  action="{!AddAttachments}" immediate="true"/>
        <apex:commandButton Value="Cancel" action="{!GoBack_toRequest}" immediate="true" />
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!lstAllAtach}" var="atch" >
        <apex:column headerValue="Select">
            <apex:inputCheckbox immediate="true" value="{!atch.Selected}" onclick="DoSelection()" />
        </apex:column>
        <apex:column headerValue="Name" value="{!atch.attachobj.Name}" />
        <apex:column headerValue="Type" value="{!atch.attachobj.ContentType}" />
        <apex:column headerValue="Size" value="{!atch.at_size}" />
        <apex:column headerValue="Date Created" >
            <apex:outputText value="{0,date, MM'/'dd'/'yyyy}" >
                <apex:param value="{!atch.at_createDate}" />
            </apex:outputText>

            <apex:image url="{!imageURL}" style="max-width:100px;max-height:100px;"></apex:image>  
        </apex:column> 
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>    

CONTROLLER

public class DelAttachments_Img_Controller {
public Id CaseId;
public selectedAttachments[] lstAllAtach {get; set;}
Public Map<Id,selectedAttachments> SelectedResultMap {get;set;}
list<Attachment> toBeDeleted {get;set;}
public String imageURL{get;set;}

public class selectedAttachments { 
    public Attachment attachobj{get; set;}
    public Boolean Selected {get;set;}
    public String at_size {get; set;}
    public DateTime at_createDate {get; set;}


}



public DelAttachments_Img_Controller(ApexPages.StandardController stdController){
    SelectedResultMap = new Map<Id,selectedAttachments>();
    CaseId = ApexPages.currentPage().getParameters().get('CaseID');
    if(CaseId != NULL)
    {
        lstAllAtach = new list<selectedAttachments>();
        List<Attachment> relatedAttachments = [SELECT Id, Name, ContentType, BodyLength, CreatedDate FROM Attachment WHERE ParentId =: CaseId];
        if(!relatedAttachments.isEmpty() && relatedAttachments != null) {
            for (Attachment a : relatedAttachments) {
                selectedAttachments oneAtch = new selectedAttachments();
                oneAtch.Selected = FALSE;
                oneAtch.attachobj = a;
                oneAtch.at_createDate = a.CreatedDate + offset;
                oneAtch.at_size = (((Decimal)a.BodyLength)/1024 >=1024.0)? (((Decimal)a.BodyLength)/1024).setScale(2)+' MB' : (((Decimal)a.BodyLength)/1024).setScale(2)+' KB';
                lstAllAtach.add(oneAtch);
                imageURL='/servlet/servlet.FileDownload?file=';
                 if(relatedAttachments.size()>0)
                  {
                      imageURL=imageURL+relatedAttachments[0].id;
                  }
            }
        }
    }
}
1

You have a single imageURL, and its not part of the list of attachments

try

<apex:image url="{!'/servlet/servlet.FileDownload?file=' + atch.attachobj.Id}" style="max-width:100px;max-height:100px;"></apex:image>

and get rid of the imageURL declaration and also

imageURL='/servlet/servlet.FileDownload?file=';
if(relatedAttachments.size()>0)
{
    imageURL=imageURL+relatedAttachments[0].id;
}
| improve this answer | |
  • Thanks Sergio Alcocer, this worked!!! – spiker k May 21 at 4:14
1

Your imageURL property is the same for all the records. You have to do the following:

a) Update your custom class to have the imageURL in

public class selectedAttachments { 
    public Attachment attachobj{get; set;}
    public Boolean Selected {get;set;}
    public String at_size {get; set;}
    public DateTime at_createDate {get; set;}
    public String imageURL{get;set;}
}

b) Update your apex so it populates properly

Remove

imageURL='/servlet/servlet.FileDownload?file=';
if(relatedAttachments.size()>0)
{
    imageURL=imageURL+relatedAttachments[0].id;
}

and instead put

oneAtch.imageURL='/servlet/servlet.FileDownload?file=' + a.id;   

c) update your VF page to pull from the class

<apex:image url="{!atch.imageURL}" style="max-width:100px;max-height:100px;"></apex:image>  
| improve this answer | |
  • Point b, shouldn't it be oneAtch.imageURL=imageURL+ a.id? – Hengky Ilawan May 20 at 21:58
  • I don’t think so. I’m copying the OP’s code and it seems to work for one image. – Sebastian Kessel May 20 at 23:10
  • 1
    You have added imageUrl as a member of the wrapper class but it was still assigned with the first Id of the list. I think that would still display the same image for all the attachments. – Hengky Ilawan May 20 at 23:31
  • Well, so it is. Good catch – Sebastian Kessel May 20 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.