• AyushKhandelwal9
  • 0 Points
  • Member since 2020

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
  • 0
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.
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];
    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>

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