This seems easy, but I'm unable to refer a static resource in a custom HTML template.The cache-control is set to public.

<img src="{!URLFOR{$Resource.Testimage}" height="100"/>

To refer to a static resource in a template dynamically, you'll need to use Visualforce templates and use a controller of some sort. Here's an example that uses Document:

public class documentImageController {
    public String fileName { get; set; }
    public String getImageUrl() {
        Document[] d = [SELECT LastModifiedDate FROM Document WHERE DeveloperName = :fileName];
        if(d.isEmpty()) {
            return null;
        } else {
            return Url.getSalesforceBaseUrl()+'/servlet/servlet.ImageServer?id='+d[0].Id+

<apex:component controller="documentImageController">
    <apex:attribute name="file" description="File Name to Render" assignTo="{!fileName}" type="String" />
    <img src="{!imageUrl}" />

And to use it:

<c:documentImage fileName="Yawning_Fox_jpg" />

If you choose to use a static resource, you could still use a Visualforce template, but only people that have logged in to your org can see it. You will either need to set up a Site and expose the resources that way. Using Document files is still the easiest way to go about your goals.

It should be {!URLFOR($Resource.Testimage)}, not as you've written it. However, HTML and plain text messages do not support URLFOR; you would need to use a Visualforce template. Additionally, users won't see it if they're not already logged in to salesforce when they view the email; static resources can't ordinarily be seen outside of Salesforce. Instead, you need to use a Document (on the Documents tab), or a File, etc. If you use a Document, make sure you check "Externally Available Image." If you do so, you'll be able to include the image URL, which has the form of (<domain>/servlet/servlet.ImageServer?id=...&oid=...&lastMod=...). Here's a fox for you from one of my dev orgs:

Fox posting a Fox from dev org

(Image from Wikipedia Commons)

  • The problem with documents is, I have to update the object id and document id with in the URL in each of the sandboxes, I was looking for a easier way to do it. – cartman Oct 13 '17 at 20:29
  • @cartman Okay, I'll need to edit this answer, one moment. – sfdcfox Oct 13 '17 at 20:34
  • @cartman Actually, why would you need this in each sandbox? Your image will be pulled from the production server every time, unless you need to change the image and preview it, in which case, you will need to use Visualforce templates so you can run some Apex Code logic. – sfdcfox Oct 13 '17 at 20:36
  • @cartman I added an alternative solution for you as well. – sfdcfox Oct 13 '17 at 21:01
  • I really appreciate your answer, but I was looking to do it using the HTMl custom template and not visualforce. If I use document url in the HTMl it would change from sandbox to sandbox. – cartman Oct 14 '17 at 0:27

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.