How to get download link/url from attachment file in salesforce?

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?

Answer

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?

Attribution
Source : Link , Question Author : LoveSalesforce , Answer Author : Keith C

Leave a Comment