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
Brandon Nelson 9Brandon Nelson 9 

Visualforce page to show attachments

I'm having an issue trying to show ID, NAME, and URL of attachment based on who uploaded it. Problem is the code is showing results even without an attachment.... Can someone please look at my code and help me understand why?
Again, I need the page to show Attachment NAME, ID, Client NAME, ID and URL to attachment.

APEX CLASS
 
public with sharing class DisplayAttachments{
public List<Account> Records {get; set;}
public DisplayAttachments(){
    String userId = userinfo.getUserId();
    Records = [select Id,Name,(Select Id,Name from Attachments) From Account Where CreatedById =:userId];
    }
}

VISUALFORCE PAGE
 
<apex:page controller="DisplayAttachments" >

    <apex:pageBlock title="{!$User.FirstName} {!$User.LastName}'s Attahments">       

    <apex:pageBlockTable value="{!Records}" var="Record">

     <apex:column >

     <apex:facet name="header">Client ID</apex:facet>

     <apex:outputText value="{!Record.Id}"/>

     </apex:column>

     <apex:column >

     <apex:facet name="header">Client Name</apex:facet>

     <apex:outputText value="{!Record.Name}"/>

     </apex:column>

     <apex:column >

     <apex:facet name="header">Attachment ID</apex:facet>

        

        <apex:pageBlockTable value="{!Record.Attachments}" var="a">

            <apex:column >

                <apex:outputField value="{!a.Id}"/>

            </apex:column>

        </apex:pageBlockTable>
         

     </apex:column>
     

     <apex:column >

     <apex:facet name="header">Attachment Name</apex:facet>

         <apex:pageBlockTable value="{!Record.Attachments}" var="a">

            <apex:column >

                 <apex:outputField value="{!a.Name}"/>

            </apex:column>

        </apex:pageBlockTable>

     </apex:column>-->

       </apex:pageBlockTable>

     </apex:pageBlock>
       
     </apex:page>

 
Tarun SuriTarun Suri
Hey Brandon ,
Hope You Doing good. As i can see in Your code You are trying to query from account to attachments thats why its showing all the account and its related attachments but if You try from attachment to account u will be good to go. here is some piece of code which will give u some idea
 
public with sharing class DisplayAttachments{
public List<Account> Records {get; set;}
Public List<Attachment> att{get;set;}
public DisplayAttachments(){
    String userId = userinfo.getUserId();
    //Records = [select Id,Name,(Select Id,Name from Attachments) From Account Where CreatedById =:userId];
    att = [select id, Name , parentId, parent.name from Attachment Where CreatedById =:userId];
    }
}
 
<apex:page controller="DisplayAttachments" sidebar="false">

    <apex:pageBlock title="{!$User.FirstName} {!$User.LastName}'s Attahments">       

    <apex:pageBlockTable value="{!att}" var="Record">

     <apex:column >

     <apex:facet name="header">Client ID</apex:facet>

     <apex:outputText value="{!Record.parentId}"/>

     </apex:column>

     <apex:column >

     <apex:facet name="header">Client Name</apex:facet>

     <apex:outputText value="{!Record.parent.Name}"/>

     </apex:column>
     
     <apex:column >
     <apex:facet name="header">Attachment ID</apex:facet>

     <apex:outputText value="{!Record.id}"/>
     </apex:column>

     <apex:column >
     <apex:facet name="header">Attachment Name</apex:facet>

     <apex:outputText value="{!Record.Name}"/>
     </apex:column>
     


       </apex:pageBlockTable>

     </apex:pageBlock>
       
     </apex:page>

Although My query will include all the objects not just account so to solve this u can add in the query 
Parentid.startsWith('001')

Thanks
Tarun Suri
(Salesforce Developer)


 
Brandon Nelson 9Brandon Nelson 9
Tarun,
Sorry for the noob question, but where would I put the Parentid.startsWith('001')? I'm an Admin and trying to learn as much as I can, but this has me lost.....
Tarun SuriTarun Suri
Hey brandon. Pls mark it as solved if it solves ur problem
att = [select id, Name , parentId, parent.name fromAttachment Where CreatedById=:userId and Parentid.startsWith('001')];