Syntax Highlighter JS

Tuesday, May 8, 2012

Fun with links in email templates has some really powerful email tools.  The most basic of which is the email template.  Currently, you can choose between these types of email templates:
  • Text
  • HTML (using letterhead)
  • Custom (without using letterhead)
  • Visualforce
 When you start out on the platform, you will most likely use the HTML based (with or without letterhead) templates.  The basic use case for email templates is to automatically notify a user when a new request comes in.   Of course, you will want to send the user a link to the request right?  If you look at the salesforce examples, you will see a seemingly easy way to get a link in your email - the .link field.   So for an email about a lead, you could just put {!Lead.Link} in your HTML template and be done.

Since Visualforce e-mail templates allow you to create PDF attachments, your next logical move will be to port your HTML templates to VF and create a PDF attachment (invoice, receipt, whatever).   

That is great for your external users's confirmation but what if you want to send a very similar email (including attachments) to your internal user and include a link?  That is when things get interesting. 

On a template related to a lead, you would think it would be as simple as {!RelatedTo.Link}.   You would be wrong and would get this error message:
invalid field link for sobject lead

Basically the problem is that this isn't an actual field and the visual force engine for email templates hasn't been configured to build a link in place of this logic.  You can easily confirm this by opening up explorer and trying to query for the field.  When you do, you will get an error like this:
So how do you get your link into your email template?  Well you could create a formula field and hard code in your Salesforce instance but that isn't portable.  The correct way is to use some visualforce creativity to make a relative link:
<apex:outputlink value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4)+relatedTo.Id}">Link</apex:outputlink>

This will work brilliantly for the HTML section of your email template but in your plain text template, you want to use just the text and not the link.   So you modify the visualforce to look like this:
<apex:outputtext value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4)+relatedTo.Id}"/>

Now you have the text of your link in the email but unfortunately you can't insert a blank line before this link (at least as far as I know) - it will appear on the same line (at least in the preview) no matter how many blank lines you put in front of it.


  1. I have got a question from your article.
    I am trying to understand, can't we use {!} in visual force templates? (or) is this only for standard object? I just confused little bit.

    When do we have to use:


    (I mean is it only when we have attachments?)

  2. Babu,

    The .Link field isn't an actual field in the database. My understanding is that the HTML email template parser will insert a link to a record for an object (standard or custom) when it sees this 'field' in its markup. As you can see from my article, this option isn't directly available in the parser for visual force email templates.

    This is most likely due to the agile development method that sales force uses.

    Until this is fixed, use the .link in HTML templates and the above outputlink for visual force email templates.

    Just note that if your visual force email template gets read by a text email client then there is a good change the formatting will be missing a blank line before the link.