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 

Email trigger to include related attachments from record

I've been working on an email trigger which will include the attachments tied to the record when the email is sent. Below is the trigger. The email gets sent however the attachments aren't included for some reason. Is there something missing?
 
trigger Send_ChecklistEmail on Food_Safety_Section_Worksheet__c (after update, before insert) {


    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 (Attachment a : [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id])
        {
        // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        efa.setContentType(a.ContentType);
        efa.setInline(false);
        fileAttachments.add(efa);
        }
        singlemail.setFileAttachments(fileAttachments);

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

 
Deepali KulshresthaDeepali Kulshrestha
Hi,

Greetings to you!


- Error is in row num 38 

for (Attachment a : [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id])

- it should be : -

List<Attachment> attList = [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id];

for (List<Attachment> a : attList)
    
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
YSPYSP
This works, thanks Deepali!
Vivek WarrierVivek Warrier

Thanks for the code! I took out the query from the for loop and placed it outside : Hope this helps a bit more!

trigger Send_ChecklistEmail on Food_Safety_Section_Worksheet__c (after update, before insert){

    if(trigger.isAfter && trigger.isUpdate){
        Map<Id,List<Messaging.Emailfileattachment>> mapFileAttachments = new Map<Id,List<Messaging.Emailfileattachment>>();
        for (Attachment a : [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId IN :trigger.newMap.keySet()]){
            // Add to attachment file list
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            efa.setContentType(a.ContentType);
            efa.setInline(false);

            if(mapFileAttachments.containsKey(a.ParentId)){
                mapFileAttachments.get(a.ParentId).add(efa);
            }
            else{
                mapFileAttachments.put(a.ParentId,new List<Messaging.Emailfileattachment>{efa});
            }
        }

        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
                if(mapFileAttachments.get(SC.Id) != null) singlemail.setFileAttachments(mapFileAttachments.get(SC.Id));

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