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
Mark VermaatMark Vermaat 

Attachment List

I am working on trying to develop a class to display just the attachments on our Salesforce Instance.  Kind of like the Attachment Manager that is available.  However, I will be having it displayed in a VisualForce Page.  Trying to query from the Database, but unsure of how to get the informaiton from all the attachments, not just related to a single case.  Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
SidharthSidharth

if you fetch body of attac hment too, you will get heap error. so avoid showing attachment body, or trim it to a small string.

 

in class create :

public List<Attachment> attchs{get; set;}

and fill this field in the class constructor

 

in vf page:

iterate over the above list using dataList.

All Answers

Mark VermaatMark Vermaat

Also, I have this as a controller, I thought it would work but it seems to not be... 

 

public with sharing class searchController{
 //page size
 private Static Final Integer PAGE_NUMBER = 10;
 
 //Search String used in ArticleList Tag
 public String searchstring {get; set;}

 public searchController(){
 String qryString = 'SELECT a.Body, a.BodyLength,a.ContentType, a.Id, a.Name, a.OwnerId, a.ParentId FROM Attachment a';
 List<Attachment> attachmentList = Database.query(qryString);
 maxSize = attachmentList.size();
 }
 
 //Keeps track of current page & max size of article list
 Integer currentPage = 1;
 Integer maxSize = 1;
 
 //returns wether we need to see previsions button or not
 public boolean getPrevRequired(){
  return currentPage > 1;
 }
 
 //Returns whether we need to see next button or not
 public boolean getNextRequired(){
  return currentPage * PAGE_NUMBER < maxSize ;
 }
 
 //Returns current page number
 public Decimal getCurrentPageNumber(){
  return this.currentPage;
 }
 
 //action for next click
 public PageReference next(){
  if(maxSize > this.currentPage * PAGE_NUMBER){
   this.currentPage = this.currentPage +1;
   }
  return null;
 }
 
 //action for previous click
 public PageReference previous(){
  if(this.currentPage > 1)
   this.currentPage = this.currentPage -1;
  return null;
 }
 
}

 

I think the issue is somewhere here: 

String qryString = 'SELECT a.Body, a.BodyLength,a.ContentType, a.Id, a.Name, a.OwnerId, a.ParentId FROM Attachment a';
SidharthSidharth

what error you getting on your vf page?

Mark VermaatMark Vermaat

Nothing, it is not returning any, and I know we have over 200,000 attachments.

SidharthSidharth

can you provide your vf page code?

Mark VermaatMark Vermaat

This is what I have as the VF page:  

 

<apex:page sidebar="false" title="Attachment List" controller="searchController">
<style>
td{
vertical-align : top;
text-align: left;
}
</style>
<apex:form >
<apex:pageBlock title="Search">
 <apex:inputtext value="{!searchstring}" id="theSearchstring" maxlength="100" size="110"/>
 &nbsp;
 <apex:commandButton value="Go" id="submitButton" style="width:30" rerender="theSearchResults"/>
</apex:pageBlock>
<apex:pageBlock title="Attachment List" >
 <apex:panelGroup id="theSearchResults" >
 <apex:panelGrid width="100%">
  <table width="99%">
   <tr>
    <th width="33%">Name</th>
    <th width="33%">Content Type</th>
   </tr>
  </table>
 //This is where everything I try to pull back the List from the controller is not working? Any code Ideas how to do this?
<apex:relatedList  title="attachmentList" pageNumber="{!currentPageNumber}" Keyword="{!searchstring}" hasMoreVar="false" pageSize="10">/>
 <table width="99%">
  <tr>
   <td width="33%><apex:outputText>{!a.Name}</apex:outputText></td>
   <td width="33%"><apex:outputText >{!a.ContentType}</apex:outputText></td>
  </tr>
 </table>
 


 </apex:panelGrid>
 <apex:panelGrid columns="2">
  <apex:commandLink action="{!previous}" value="Previous" style="{!IF(prevRequired =
   true,'display:block','display:none')}" reRender="theSearchResults"/>
  <apex:commandLink action="{!next}" value="Next" style="{!IF(nextRequired =
   true,'display:block','display:none')}" reRender="theSearchResults"/>
  </apex:panelGrid>
  </apex:panelGroup>
 </apex:pageBlock>
</apex:form>
</apex:page>

 Any help would be great

SidharthSidharth

class:

 

public with sharing class sid_attachment{
//page size
private Static Final Integer PAGE_NUMBER = 10;

public List<Attachment> attchs {get; set;}

//Search String used in ArticleList Tag
public String searchstring {get; set;}

public sid_attachment(){
String qryString = 'SELECT a.Name FROM Attachment a limit 500';
attchs = Database.query(qryString);
maxSize = attchs.size();
}

//Keeps track of current page & max size of article list
Integer currentPage = 1;
Integer maxSize = 1;

//returns wether we need to see previsions button or not
public boolean getPrevRequired(){
return currentPage > 1;
}

//Returns whether we need to see next button or not
public boolean getNextRequired(){
return currentPage * PAGE_NUMBER < maxSize ;
}

//Returns current page number
public Decimal getCurrentPageNumber(){
return this.currentPage;
}

//action for next click
public PageReference next(){
if(maxSize > this.currentPage * PAGE_NUMBER){
this.currentPage = this.currentPage +1;
}
return null;
}

//action for previous click
public PageReference previous(){
if(this.currentPage > 1)
this.currentPage = this.currentPage -1;
return null;
}

}

 

 

 

page:  

 

<apex:page sidebar="false" title="Attachment List" controller="sid_attachment">
<style>
td{
vertical-align : top;
text-align: left;
}
</style>
<apex:form >

<apex:pageBlock title="Attachment List" >
<apex:panelGroup id="theSearchResults" >
<apex:panelGrid width="100%">
<apex:dataList var="a" value="{!attchs}" type="1">
{!a.name}
</apex:dataList>
</apex:panelGrid>
<apex:panelGrid columns="2">
<apex:commandLink action="{!previous}" value="Previous" style="{!IF(prevRequired =true,'display:block','display:none')}" reRender="theSearchResults"/>
<apex:commandLink action="{!next}" value="Next" style="{!IF(nextRequired =true,'display:block','display:none')}" reRender="theSearchResults"/>
</apex:panelGrid>
</apex:panelGroup>
</apex:pageBlock>
</apex:form>
</apex:page>

SidharthSidharth

if you fetch body of attac hment too, you will get heap error. so avoid showing attachment body, or trim it to a small string.

 

in class create :

public List<Attachment> attchs{get; set;}

and fill this field in the class constructor

 

in vf page:

iterate over the above list using dataList.

This was selected as the best answer
Mark VermaatMark Vermaat

Thank you Sidharth, that has corrected most of my issues.  I appreciate your help.