Pages

Tuesday, April 3, 2018

Salesforce: Formula Field to view Attachment

This blog is more for Classic, as the attachment is no longer support in Lightning, although existing attachments will be still available in Lightning, you just can't upload the new attachments in Lightning.

Here is the requirement, users would like to have the ability to view the attachment within one click from record page layout, assume only max 1 attachment per record.
By default, you need to scroll down to the "Notes & Attachments" related list, then click the file name, this will open attachment page (00Pxxxxxx), which also show the file size, then click "View file" link -- this is two click from the record page.

Using formula field below, it will show View link when the "id" is available, otherwise, the View link field will not be visible.

IF (NOT(ISBLANK(Attachment_Id__c)),HYPERLINK("/servlet/servlet.FileDownload?file="+ Attachment_Id__c,"View", "_blank"),NULL)

This is to assume that Attachment_Id__c value (a custom text field) has been populated by apex trigger.

The above formula field will work in Lightning, but "_blank" target will not work; the image will overwrite existing web browser window.

Jeff's blog How to Customize Salesforce Attachments explains how to "add" custom fields for Attachment using a custom object, act as a layer between the real object with attachment.