+ Start a Discussion
Priyanka7Priyanka7 

Display Attachment of Custom Object A using Visualforce Page

Hi All,
I have a requirement to show the attachment of Object A using Visualforce page and add it to Custom Button. Whenever the button is clicked the Attachment of a particular record should be shown as pdf.

Please help me out...!
Thanks in advance.

NagendraNagendra (Salesforce Developers) 
HI Priyanka,

Tweak the below code as per your requirement to display the same as PDF.

The below code displays the attachment using a visual force page.
Use the below controller and Visual force page  :


Apex Class

Public Class displayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;    
    }
}

 

 

Visual Force Page

 

<apex:page standardController="Account" extensions="AccountImageController">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>

 

Please let me know If you need any help regarding the same .
Hope this helps.

Mark this as solved if the reply was helpful.

Thanks,
Nagendra