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
YSPYSP 

Trigger to include related files on email.

Hello,

I am working on an email trigger which will also attach the related files to the email. I have included the code I put together below. When I try to trigger the email I recieve an error stating the list has no rows for assignment to SObject in line 50 (contentversion cv = [Select Id, ContentDocumentId, VersionData, PathOnClient, FileType from ContentVersion where ContentDocumentId = :cd.Id];).

I was originally using ContentDocument instead of ContentDocumentLink however when the email fired none of the related files were included. Is there something I'm doing wrong?
 
trigger Send_ChecklistEmail on Food_Safety_Section_Worksheet__c (after update) {


    for(Food_Safety_Section_Worksheet__c SC : trigger.new){
        Food_Safety_Section_Worksheet__c oldSC = Trigger.oldMap.get(SC.Id);
        if(SC.Send_Email__c == true && SC.Send_Email__c != oldSC.Send_Email__c){
            
            //Retrieve Email template
            EmailTemplate et=[Select id from EmailTemplate where name=: 'Food Safety Email'];
            
            //Create email list
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();            
            
            //Create message
            Messaging.SingleEmailMessage singlemail = new Messaging.SingleEmailMessage();
            //add template
            singlemail.setTemplateId(et.Id);
            //set target object for merge fields
            singlemail.setTargetObjectId(SC.OwnerId);
            singlemail.setWhatId(SC.Id);
            //set to save as activity or not
            singlemail.setSaveAsActivity(false);
            
            //add address's that you are sending the email to
            List<String> sendTo = new List<String>();
            String owneremail = [select Email from User where Id = :SC.OwnerId].Email;
            sendTo.add(SC.Franchisee_Email__c);
            sendTo.add(owneremail);
            
            //set addresses
            singlemail.setToAddresses(sendTo);
            
            //add mail
            emails.add(singlemail);
        
            //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

for( ContentDocumentLink cd : [select Id, LinkedEntityId from ContentDocumentLink where LinkedEntityId =  :SC.Id]){ 
contentversion cv = [Select Id, ContentDocumentId, VersionData, PathOnClient, FileType from ContentVersion where ContentDocumentId = :cd.Id];
        
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(cv.PathOnClient);
efa.setBody(cv.VersionData);
efa.setContentType(cv.FileType);
efa.setInline(false);
fileAttachments.add(efa);
}
singlemail.setFileAttachments(fileAttachments);
       

        //Send email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{singlemail});
 }  
}
}
Thanks!