function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bathybathy 

Add a dynamic image to a visual force email template

Hi Guys,

I have a visual force email template and I need to add dynamic images to that template to send to a contact. Can we have the images attached to the records in the Notes and Attachments section and then run a query and insert all the required images in the email template?

Please advise.

Thanks,
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Bathy,

This is possible. Refer your image like below on page.
URLFOR($Action.Attachment.Download, AttachmentId)
Let us know if it helps you.
 
bathybathy
Hi Ashish,

Thanks for your reply. I place this in the <img src="URLFOR($Action.Attachment.Download, AttachmentId)" />  but the image stored as an attachment is not displayed.
any work around for this?

Thanks,
 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Bathy,

Please try below line .Please use your atachment id.
<apex:image url="/servlet/servlet.FileDownload?file=YOUR_ATTACHMENT_ID"/>

Let us know if it helps you.
bathybathy
Hi ashish, Thanks for your quick reply. How can I retrieve the attachment id? Should I use a soql query? Please let me know I haven't doe this type of template before. Thanks
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Bathy ,


To see it working ,just hardcode attachment id and then you can query it using SOQL.

Please try below code.
Public Class DisplayImageExtension {

    String recId;
    
    public displayImageExtension(ApexPages.StandardController controller) {
        recId = controller.getId();    
    }
    
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;    
    }
}
 
 
Visual Force Page
 
<apex:page standardController="Account" extensions="DisplayImageExtension">
<apex:form >
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>
</apex:form>
</apex:page>
Let us know if it helps you.

 
bathybathy
Hi Ashish, Thanks for your reply. sorry for asking, how can I use this extension in VF email Template? Regards,