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 

Creating a VIEW link for Attachments

I have created a Attachment Manager type setup, and the only thing is I am not able to pass the ID of the attachment to a new window.  It opens with nothing in it.

 

Any idea how I could fix this maybe?

 

   <apex:actionStatus id="status" startText="Searching... please wait..."/>
      <apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
        <apex:pageBlockTable value="{!searchResults}" var="item" rendered="{!NOT(ISNULL(searchResults))}">
        <apex:column headerValue="Delete">
            <apex:commandLink action="{!deleteAttachment}" onclick="if(!confirm('Are you sure?')) return false;">Del
                <apex:param value="{!item.id}" name="idToDel" assignTo="{!AttachId}"/>
            </apex:commandLink>
            </apex:column>
            <apex:column value="{!item.id}" HeaderValue="Attachment ID" width="100"/>
          <apex:column headerValue="Name of Attachment   (Click to view)" >
            <apex:outputLink value="{!URLFOR($Action.Attachment.Download,item.id)}" target="_blank">
            View</apex:outputLink>
            </apex:column>

 The code displays the list, as well as the option to Delete it, (and it works) however when I click on the View, it opens a new window but nothing is pulled over to view.  ?? Any idea?

sunil_kumarsunil_kumar

Hi Mark,

 

If you have to used $Action Global Variable for attachment, then standard controller should be "Attachment".

below is sample code

<apex:page standardController="Attachment">
    <apex:outputLink 
      value="{!URLFOR($Action.Attachment.Download,attachment.id)}">Download Now! </apex:outputLink> </apex:page>

Hope this will help you.

[If it solves your problem, please mark it as solution]

 

Mark VermaatMark Vermaat

Hey Sunil,

 

I have already set it as the controller.  See full VF page code below.

 

<apex:page standardController="Attachment" extensions="ItemEditController">
  <apex:sectionHeader title="{!Attachment.Name}" subtitle="Edit Attachments"/>
  <apex:form >
    <apex:pageBlock mode="edit" id="block">
 
      <apex:pageBlockButtons location="both">
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />
 
      <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
          <apex:outputLabel for="searchText">Keyword</apex:outputLabel>
          <apex:panelGroup >
          <apex:inputText id="searchText" value="{!searchText}"/>
          <apex:commandButton value="Search" action="{!search}" rerender="block" status="status"/>
          </apex:panelGroup>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection><br/>
 
      <apex:actionStatus id="status" startText="Searching... please wait..."/>
      <apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
        <apex:pageBlockTable value="{!searchResults}" var="item" rendered="{!NOT(ISNULL(searchResults))}">
        <apex:column headerValue="Delete">
            <apex:commandLink action="{!deleteAttachment}" onclick="if(!confirm('Are you sure?')) return false;">Del
                <apex:param value="{!item.id}" name="idToDel" assignTo="{!AttachId}"/>
            </apex:commandLink>
            </apex:column>
            <apex:column value="{!item.id}" HeaderValue="Attachment ID" width="100"/>
          <apex:column headerValue="Name of Attachment   (Click to view)" >
            <apex:outputLink value="{!URLFOR($Action.Attachment.Download,item.id)}" target="_blank"> 
            View</apex:outputLink>
            </apex:column>
            
          <apex:column value="{!item.Name}" headerValue="Item" width="100"/>
          <apex:column value="{!item.ContentType}" headerValue="Type" width="100"/>
          
          
        </apex:pageBlockTable>
      
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 So thanks for your reply, however i am still seeing the issue.

 

Any other suggestions.

sunil_kumarsunil_kumar

Hi Mark,

 

Replace item id in<apex:outputlink value="{!$Action.Attachment.download,item.id }"/> with Attachment.id

After doing this, when you click on link it will display the attachment whose id is passed in URL.

 

You have used outputlink in pageblocktable. So it means you have List of attachment. Standard controller takes care of only single records whose id is passed in URL. If you iterating through list of attachments then replace the code with below code

 

<apex:outputlink value="/servlet/servlet.FileDownload?file={!item.id}" target="_blank">View</apex:outputlink>

 

 

Hope this will help you.

[If it solves your problem, please mark it as solution]

Shalini RShalini R
Hi,
I have used the above code working fine. Thanks for your post. Is it possible to open attachment on new tab.