[SalesForce] display link to download an attachment in an email

Scenario –
Case object has a custom child object (sample_case_child__c). That child object will have some file attachment.

When a case is sent for approval, the email sent out to approver should show the link to download the attachment on the child object.

I am using visualforce email template for this.

Template has a component, which does the work of querying for attachment and putting the output link.

My problem is, in the email that is sent, there is no hyperlink. The text "Download here" appears, but it is not a hyperlink. What am i doing wrong?

Code for VF template –

<messaging:emailTemplate subject="test" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
     <c:showChildObject parentCaseId="{!relatedTo.Id}"/> 
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Code for the component showChildObject –

<apex:component access="global" controller="showChildObjectController">
<apex:attribute name="parentCaseId" type="Id" assignTo="{!currentCaseId}" description="test description"/>
<apex:outputLink value="{!linkForAttachment}"> Download here</apex:outputLink>
</apex:component>

Controller for the component –

public class showChildObjectController{
    public Id currentCaseId {get; set;}

    public String getlinkForAttachment(){
      sample_case_child__c result = [Select Name, Amount__c, (Select Id, Name from Attachments) from sample_case_child__c Where Case__c=:currentCaseId LIMIT 1];
      Attachment temp = result.Attachments[0];
      String link = '{!URLFOR($Action.Attachment.Download,'+ String.valueOf(temp.Id)+')}';
      return link;
    }
}

Best Answer

There is apparently some error with outputLinks in html email templates. Try to use anchor tag instead like this:

<apex:component access="global" controller="showChildObjectController">
<apex:attribute name="parentCaseId" type="Id" assignTo="{!currentCaseId}" description="test description"/>
<a href="{!linkForAttachment}">Download here</a><br/>

</apex:component>

The first line works for me

Related Topic