+ Start a Discussion
Rachid El KaddouriRachid El Kaddouri 

Trigger to get download link/url from attachments in salesforce?

Hi,

I'm trying to write a trigger that will give me the URL to an attachment on a custom object. 
I made a formula text field on a custom object with this formula:

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

But the second step is difficult and I can't seem to get it to work. I want a link of the attachment to display. I want to create a after insert trigger but I can't get it to work.. I hope someone knows a solution for this..

Kind regards

Best Answer chosen by Rachid El Kaddouri
logontokartiklogontokartik
Maybe its just your business problem, but it really is of no use to have a link as custom field and show up on the List. Because when User is ready to click on the Visualforce Page link, he would rather click on the Record to open the detail view right? 

Anyways, for that you can create a formula field and add
HYPERLINK('/apex/{yourvisualforcename}?id='+ Id, 'Images Preview')


All Answers

logontokartiklogontokartik
You can have more than 1 attachment on any object and you already have a View link that either downloads or opens based on your browser on attachments. May I know the specific use case why you want to have a link on Custom Object so that I can provide much optimal response.

Thank you
Rachid El KaddouriRachid El Kaddouri
Hi,
Thank you for your reaction.

I have a custom object(Employee) where employees upload their receipts in the Notes & Attachments section of that custom object (Employee). But if the managers make a view of the records within the custom object (Employee) the Notes & Attachments can't show up in the view. So I created a formula text field with a link/url to the image the employees uploaden with Notes & Attachments. The formula above works but only for one attachment.. I want this to be dynamically so that for every record the image can show up in the custom field with formula..

I hope the story above is understandable.. @logontokartik

Thank you in advantage..

Cheers
 
logontokartiklogontokartik
I am not sure if you can use formula that way to get all the Attachments show up as images. You can maybe use Visualforce Soln. for your problem.

Create a Visualforce Page that you can add as section on your Employee Object, that will show the preview of all the Attachment Images .

Below is the Sample I created for you. (Change the Test_Object__c to your Custom Object)

<apex:page standardController="Test_Object__c" extensions="AttachmentImagesExt" sidebar="false" showHeader="false">
	<apex:repeat value="{!imageAttachments}" var="image">
    	
        <a href="/servlet/servlet.FileDownload?file={!image.Id}" target="_blank">
            <img src="/servlet/servlet.FileDownload?file={!image.Id}" width="100" height="75"/> 
        </a>&nbsp;    
    </apex:repeat>
</apex:page>

Here is the Controller - In the controller you can also filter attachments based on Content Type (png, jpg, jpeg etc).

public with sharing class AttachmentImagesExt{
    public List<Attachment> imageAttachments {get;set;}
    public AttachmentImagesExt(Apexpages.StandardController con){
        imageAttachments = new List<Attachment>();
        imageAttachments = [Select Id, Name, ContentType from Attachment where ParentId = :con.getId()];
    }
}


Add the Visualforce Page as section to your Employee Detail page. and you should see all the images shown up on it. You dont need to write a trigger or manage additional fields now. And the page is resuable on any Object.


Hope this helps.
ouz_alpouz_alp
Hi Rachid , 
Since the hyperlink will refer to only one attachment you can show the attachment just for one (ex. last created attachment) ,There is no way to do that with one hyperlink , you need to create a visualforce page which shows the all attachments of the specific Employee with a downloadable outputlink. 
logontokartiklogontokartik
I am not sure what you meant, but if you want to add a link to the Visualforce page on the Detail Record, create a Custom Link on the Object and choose the Visualforce page.

The page is generally accesible from URL  /apex/{yourvisualforcepagename}

You can then display the custom link on the Page Layout in the Custom Links section.

Please mark the above as solved if it solved your problem.

Thank you.
logontokartiklogontokartik
Maybe its just your business problem, but it really is of no use to have a link as custom field and show up on the List. Because when User is ready to click on the Visualforce Page link, he would rather click on the Record to open the detail view right? 

Anyways, for that you can create a formula field and add
HYPERLINK('/apex/{yourvisualforcename}?id='+ Id, 'Images Preview')


This was selected as the best answer
Sravanthi SSravanthi S
Hi @logontokarthik - Is your solution / VF page for viewing attachments in a single click from case details page?

I am looking to build a trigger/field/link which takes me to Attachments from the emails.

My question - https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Visualforce_Development&criteria=OPENQUESTIONS&id=906F0000000BWk3IAG 
RONURONU
Hi Kartik,
I have one requirement where I am storing attachments in notes and attachments section of any record in Template custom object. After that I also want to have a custom URL type field in some custom object . The value will be a direct link to download the attachment file. Whenever any user clicks the link, the attachment should get dowloaded.
I know we can do with the custom formula field, but struggling to get the exact URL code for it.

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