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
Nehashri320Nehashri320 

Send emails with attachments in a Trigger

Hi forum,
   I have an after insert trigger on Child object SAFETY_Brief and the parent object is Master_safe_brief.Whenever a safety_brief record is created, i would like to send email to an email field Assigned_email which is on Safety_brief and also the attachments present on the parent objetc Master_safe_brief of that safety_brief.
  Below is my code but i need help regardingemail.setToAddresses(sendTo); where its is supposed to iterate to each e=assigned email of differnt safe briefs  and send emails but its sending emails multiple times to the first safety brief inserted, and so on. I am also attaching the debug log for email queue.User-added image
public class SafetyBriefAttachment {
    public static void sendEmailAttachment(List<Safety_Brief__c> safeBrief){
        Set<Id> masterSafe = new Set<Id>();
        for(Safety_Brief__c sb : safeBrief){
            masterSafe.add(sb.Master_Safety_Brief__c);
        }
        Map<Id, Master_Safety_Brief__c> masterSafeBriefMap = new Map<Id, Master_Safety_Brief__c>();
        for(Master_Safety_Brief__c mastersafety : [select id,Name, Weekly_Topic__c, Publish_Date__c, Due_Date__c from Master_Safety_Brief__c where Id IN : masterSafe]){
            masterSafeBriefMap.put(mastersafety.id, mastersafety);
        }
        
        List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
        List<Messaging.SingleEmailMessage> dummyemailList = new List<Messaging.SingleEmailMessage>();
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        List<String> sendTo = new List<String>();
        List<Messaging.EmailFileAttachment> attachmentList = new List<Messaging.EmailFileAttachment>();
        //Query all the Parent Object Attachment and put into a MAP.
        Map<Id, List<Attachment>> parentAttacmentMap = new Map<Id, List<Attachment>>();
        for(Attachment att: [SELECT id, ParentId, Name, body, ContentType FROM Attachment WHERE ParentId IN :masterSafe ]){
            if(parentAttacmentMap.containsKey(att.ParentId)){
                parentAttacmentMap.get(att.ParentId).add(att);
            }else{
                parentAttacmentMap.put(att.ParentId, new List<Attachment>{att});
            }
        }

        for(Safety_Brief__c sb : safeBrief){
            if(sb.Assigned_Email__c != null){
               
                String Name = sb.Name;
                String subject = 'SAFE BRIEF';
                email.setSubject(subject);
                email.setHtmlBody('<html><head><img src="https://riskonnectrmk--c.na42.content.force.com/servlet/servlet.ImageServer?id=015F0000003wzGV&oid=00DF00000008OmQ" alt="logo" /></head>'+
                                  '<body style="font-family: arial; font-size: 12pt;"><p>Attached is this week\'s SAFE Brief.</p>'+'\n'+
                                  '<b>Weekly Topic:</b> '+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Weekly_Topic__c+', '+
                                  '<b>Publish Date:</b> '+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Publish_Date__c.day()+'/'+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Publish_Date__c.month()+'/'+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Publish_Date__c.year()+', '+
                                  '<b>Due Date: </b>'+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Due_Date__c.day()+'/'+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Due_Date__c.month()+'/'+masterSafeBriefMap.get(sb.Master_Safety_Brief__c).Due_Date__c.year() +'\n'+
                                 '<p>Click the link below to complete the SAFE Brief for this week in the GMM.:</p>'+ '\n'+
                                  '<P><u>'+ URL.getSalesforceBaseUrl().toExternalForm() +'/' + sb.id+ '</u></P></body></html>');
                
                sendTo.add(sb.Assigned_Email__c);
                
                email.setToAddresses(sendTo);
                System.debug('Email address :'+sendTo);
                // Traverse on Attachment on Parent Record and attach it in email. 
                for(Attachment att: parentAttacmentMap.get(sb.Master_Safety_Brief__c)){
                    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                    efa.setFileName(att.Name);
                    efa.setBody(att.Body);
                    efa.setContentType(att.ContentType);
                    efa.setInline(false);
                    attachmentList.add(efa);
                 }
                 if(attachmentList != null && attachmentList.size() >0)  
                     email.setFileAttachments(attachmentList);
                    //emailList.add(email);
                    Messaging.sendEmail(email);
            }
        }
        /*if(emailList != null && emailList.size() > 0)
            Messaging.sendEmail(emailList);*/
        System.debug('You have made '+Limits.getEmailInvocations()+'emails calls out of'+Limits.getLimitEmailInvocations()+ 'allowed');
        
    }
}

Thank you