You need to sign in to do that
Don't have an account?
AyushKhandelwal9
Dynamically Attach the file with VF email Template
I want to send the email to a user of my org whenever a case record is marked as closed. I want to add the recent file as attachment with the case. I have a VF email template which I want to use to send the emails.
We have an option to do this via apex(SingleEmailMessage) but I do not want to add user email as toAddress(). It might be counted as the daily email limit.
I have created a VF component to add the attachment but the attachment is not coming in readable format. VF is not rendering EncodingUtil.base64Encode(cv.VersionData) in readable format in attachment.
I have tried to use toString() method as well but it only works if the file contains text else it will throw an error (blob is not a valid utf-8 string).
Please help me with this issue
We have an option to do this via apex(SingleEmailMessage) but I do not want to add user email as toAddress(). It might be counted as the daily email limit.
I have created a VF component to add the attachment but the attachment is not coming in readable format. VF is not rendering EncodingUtil.base64Encode(cv.VersionData) in readable format in attachment.
public List < String > getFile() { List < ContentDocumentLink > cdl; List < String > fileData = new List < String > (); ContentVersion cv; String fileName = fileNameLabel + '%'; if (caseRecordId != null) { cdl = [Select Id, ContentDocumentId, LinkedentityId, ContentDocument.Title FROM contentDocumentLink where LinkedEntityId =: caseRecordId and ContentDocument.Title like: fileName limit 1]; } if (cdl != null && !cdl.isEmpty() && cdl[0].ContentDocumentId != null) { cv = [Select Id, Title, VersionData, FileType from ContentVersion where ContentDocumentId =: cdl[0].ContentDocumentId]; } fileData.add(cv.Title); fileData.add(cv.FileType); fileData.add(EncodingUtil.base64Encode(cv.VersionData)); return fileData; }
<apex:component controller="AttachFileController" access="global"> <apex:attribute name="caseRecId" type="Id" description="Id of the case" assignTo="{!caseRecordId}" /> <apex:attribute name="fileName" type="String" description="File Name" assignTo="{!fileNameLabel}" /> <messaging:attachment rendered="{!File.size > 0}" renderAs="{!If(File.size > 0, File[1], 'pdf')}" filename="{!If(File.size > 0, File[0], 'Attachment')}"> {!File[2]} </messaging:attachment> </apex:component>
I have tried to use toString() method as well but it only works if the file contains text else it will throw an error (blob is not a valid utf-8 string).
Please help me with this issue
Please try with below : Make sure to replace caseOwnerId with the appropriate user ID, emailTemplateId with the ID of your VF email template, fileContent with the binary content of the file, and fileName with the desired name of the attachment.
By using this approach, you can send an email with an attachment without directly specifying the recipient's email address in toAddress().
Hope this helps !
Thank you.