I've created a Custom Email Template. In the HTML Email Content, a few fields are displayed and I've created a button with a link

<a href="https://c.cs15.visual.force.com/apex/PortalCasePage?id={!Case.Id}" class="btn-Open-Case">Open Case</a>

to direct the customer to the Portal Case Page. This is working in one environment. To make this work in the other environments where the URL changes, I'm trying to make use of the getSalesforceBaseUrl(). We have an URL helper class that returns the appropriate URL. But how do I merge that in this HTML link? Thanks in advance.


To get the string value of the base url use:

String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm()

then append the '/' + your other parameters you may need

Assuming your URL helper class is not named URL

Also, you will probably need to change to a VF email template. Look for other answers though that may have a way to do it in the HTML template. (One clunky way is to populate a field on the case that holds the base URL and use a trigger to set the value. Then you could use that in the custom HTML email template. Or use a custom setting)

| improve this answer | |
  • I believe he is using a Custom HTML email template and not Visualforce email.. will he be able to access apex from custom html emails ? – Vamsi Krishna Aug 26 '14 at 1:18
  • Ahh, need to read closer, I believe the OP will need to change to a VF template. – Eric Aug 26 '14 at 1:26
  • Thank you @Eric. Yes, I came up with two approaches. First, by using custom settings. And second, like you said, setting the value of a field by using a trigger. But I was curious to know if we can pull the baseURL from the HTML Email Content of a custom template. – Seesh Aug 26 '14 at 3:12
  • You can use System.URL.getSalesforceBaseUrl().toExternalForm() if you name your class URL – Scott Pelak May 24 '17 at 21:25
  • @ScottPelak - If you have a class named URL or any class using the same name as a system method it is highly encouraged to change it. – Eric May 24 '17 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.