14

Hi I have one requirement where I am storing attachments in attachment object. After that I also want to update a custom url type field in some custom object . The value will be a direct link to download the attachment file.

How to do that? Any idea. I have access to all the fields in attachment record. How to get the download link from that?

1 Answer 1

13

When requested from a valid session (i.e. after someone has logged in to salesforce) this URL (that includes the ID of the attachment) works:

/servlet/servlet.FileDownload?file=00PM0000001fH6h

If you create a text formula field on an object of this form:

HYPERLINK('/servlet/servlet.FileDownload?file=' + AttachmentId__c, 'View', '_blank')

the link can be displayed in default (layout based) UI. You will have to use an Attachment after trigger to store the AttachmentId on the object. The Attachment's ParentId tells you what its immediate parent object is and you can query as needed from there to find the object if it is not the immediate parent. (You need to decide what you want to do when there are multiple Attachments.)

But if you want to say email the link to a user that is not logged in to salesforce, you will need to use an "externally available" Document not an Attachment (though you could copy the Attachment content to a Document automatically in a trigger). Details on how to do this are provided in this blog post Salesforce: How to make a Document public?. This mechanism works for e.g. PDF as well as images.

PS

For Content (File) objects see What is the difference between these 2 download URLs?

6
  • 10
    Some supplemental information: in VF markup, you generate the link using URLFOR() and the $Action global variable rather than hardcoding the path to the servlet. <apex:outputLink value="{!URLFOR($Action.Attachment.Download, yourAttachment.id)}"> Documentation: $Action
    – Mark Pond
    Oct 1, 2013 at 23:17
  • 1
    Thanks Mark. This is a better way to go if the link is only in VF: not so fragile and no mixing of view information with the model. (Though the hard-coded segment could come from a custom setting to provide a little protection.) The link field is useful where default layout-based UI is being used and can appear in both detail panels and related list panels.
    – Keith C
    Oct 2, 2013 at 7:24
  • 1
    Agree URLFOR would is preferable when it works (within a logged-in session), however, in a VF Email template, I've found that the generated url doesn't include the orgid needed. Would be nice to have a way to generate that url cleanly. Oct 2, 2013 at 14:26
  • @MarkPond Is there any way to create a link for Files with ContentDocument Id or ContentVersion Id or any other related object id? Feb 5, 2020 at 13:28
  • @AdityaVijay See the PS I just added.
    – Keith C
    Feb 5, 2020 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .