+ Start a Discussion
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN 

how to display all the available attachements on vfpage

Hi All,

Please help me on the below 

I have a VFpage where in one section i need to display all the avaliable attachments for a record(FYI i have record ID for the partitcular record lets say recId).

Please help me with sample code in order to acheive this also on click of the attachment record i should be able to download the attachment.

BEst Regards,
Mohammad Yaseen
Best Answer chosen by SHAIK MOHAMMAD YASEEN
Deepankar ChandaDeepankar Chanda
Hello Shaik,

Quickly what I can tell you where to go to get all the attachments and what field will help you to join between your parent record & attachment 

SELECT id, parentid, name FROM Attachment

Parentid is foreign key to your parent record
Name is the name of the file you uploaded to the salesforce.
Id is the record id of the attachment which you have to bind with the below url to download the content:

Query Output

https://orgname-dev-ed--c.ap2.content.force.com/servlet/servlet.FileDownload?file=00P2800000cihAC

I hope the mentioned information help you to figure out to develop the section in your VF page
 

All Answers

Deepankar ChandaDeepankar Chanda
Hello Shaik,

Quickly what I can tell you where to go to get all the attachments and what field will help you to join between your parent record & attachment 

SELECT id, parentid, name FROM Attachment

Parentid is foreign key to your parent record
Name is the name of the file you uploaded to the salesforce.
Id is the record id of the attachment which you have to bind with the below url to download the content:

Query Output

https://orgname-dev-ed--c.ap2.content.force.com/servlet/servlet.FileDownload?file=00P2800000cihAC

I hope the mentioned information help you to figure out to develop the section in your VF page
 
This was selected as the best answer
v varaprasadv varaprasad
Hi Yaseen,

P​lease use below code if we will select Account in contact vf page it will display related attachments.
VF page : 
<apex:page standardController="Contact" extensions="loadAccountAttachments">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!contact.Lastname}"/>
                <apex:inputField value="{!contact.Phone}"/>
                <apex:inputField value="{!contact.Accountid}"> 
                    <apex:actionSupport event="onchange" action="{!loadAttachments}" reRender="atsBlock"/>
                </apex:inputField>
            </apex:pageBlockSection>           
            
            <apex:pageBlockSection title="Attachments" columns="1" id="atsBlock">
                <apex:pageBlockTable value="{!Attachments}" var="a">
                    <apex:column headerValue="Type">Attachment</apex:column>         
                    <apex:column headerValue="Title">
                        <a href="/servlet/servlet.FileDownload?file={!a.id}" download="{!a.name}">{!a.name}</a> 
                    </apex:column>             
                    <apex:column headerValue="Created By" value="{!a.CreatedById}"/>                    
                </apex:pageBlockTable>                
            </apex:pageBlockSection>   
        </apex:pageBlock>
    </apex:form> 
</apex:page>
Class : 
public class loadAccountAttachments {   
    public list<attachment> Attachments{get;set;}
    public id accountid{get;set;}
    public contact cont;
    
   public loadAccountAttachments(ApexPages.StandardController controller){
        cont = (contact)controller.getRecord();        
        system.debug('==accountid=='+cont.AccountId);
    }
   
    public void loadAttachments(){
        system.debug('==accountid=='+accountid);
         Attachments = [SELECT id,CreatedById,Name FROM Attachment where ParentId =:cont.AccountId LIMIT 50000]; 
        system.debug('==Attachments=='+Attachments);
    }
}

Hope This helps.
Please check once and let me know if any help needed.

Thanks
Varaprasad​