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
utz1utz1 

Not able to send email with attachment

Hello,
I am writing trigger where i want to send email to related cases when email is sent to parent cases. well, I am able to send mail to related cases with plian body and tempaltes. but not able to send attachment to the related case. Is there any way to send email with atatchement?

trigger Sendemailmessage on EmailMessage (after insert)
{
        system.debug([select id,(select id from attachments) from emailmessage where id in : Trigger.new]);

Set<Id> contactIds = new Set<Id>();
EmailMessage ems = trigger.new[0];
  /*   set<id> eid = new set<id>();

       for(EmailMessage ems: trigger.new){
           if(ems.ParentId != null) 
           {
        eid.add(ems.ParentId);
           }
    } */

if(ems.ParentId != null)  
 {
Case c = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
               FROM Case where Id =: ems.ParentId];   
system.debug('List of Case :='+c);
 
Case[] caseId = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where ParentId=: c.Id];
   system.debug('List Related Child Case '+caseId);
     
     EmailMessage em =    [select id, ccaddress, BccAddress, FromAddress, Subject, ActivityId,ToAddress,ParentId,HasAttachment, HtmlBody, TextBody
                          from EmailMessage where ParentId=: c.Id  order by CreatedDate desc limit 1];
     system.debug('Case Ids to get email message '+em);
     system.debug('em.ParentId:----->'+em.ParentId);
     system.debug('Email Message Id:---->' +em.Id);
         
   
List<Attachment> attachments = new List<Attachment>();

    if(em.HasAttachment)
    {
  
   attachments  = [SELECT Id, Name, Body, BodyLength,ContentType, ParentId from Attachment  where ParentId=:em.Id];
  //   system.debug('List Of attachement.size:--->'+attachments.size());
         // String attachments1 = [SELECT Id, Name, Body, BodyLength,ContentType, ParentId from Attachment  where ParentId=:em.ParentId].ParentId;


     system.debug('List Of attachement:--->'+attachments);
    }
        for (Case c1 : caseId)         
            //for (Case c1 : Trigger.new) 

  {  

     contactIds.add(c1.ContactId);
      system.debug('c1.ContactId:----->'+c1.ContactId);
     Map<Id,String> ContactEmail = new Map<Id, String>();
     Map<Id, String> ContactFirstname = new Map<Id, String>();
     
     for(Contact c2: [select id, email from Contact where id IN :contactIds])
    {
        system.debug('c2:'+c2);
        
      ContactEmail.put(c2.id,c2.email);
        system.debug('c2.id:---->'+c2.id);
        system.debug('c2.email:---->'+c2.email);
        
      if (em.id != Null) 
  {
      system.debug('c1.CaseNumber:---->'+c1.CaseNumber);

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      String[] toAddresses = new String[] {ContactEmail.get(c2.id)}; 
      mail.setToAddresses(toAddresses);
      mail.setSubject(em.Subject);
      mail.setUseSignature(false);
      string msg = '';
      msg = msg +em.HtmlBody;
      mail.setHtmlBody(msg);
     // mail.setCcAddresses(ccAddresses);
     //mail.setReplyTo(toaddress);
    // mail.setSenderDisplayName('Name');
     mail.setBccSender(false);
    if(attachments.size() > 0)
      {
      
      List<Messaging.EmailFileAttachment> emailAttachments = new List<Messaging.EmailFileAttachment>();
for(Attachment att : attachments) {

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    // You may need to do some work here to figure out the correct file extension
    efa.setFileName(att.Name);
    // Take the blob data from the Attachment and put it on the email.
    efa.setBody(att.Body);
    efa.setContentType(att.ContentType);
    emailAttachments.add(efa);
}

mail.setFileAttachments(emailAttachments);
   //     mail.saveAsActivity = True;
   mail.setSaveAsActivity(True);

      } 
      
       // Send the email
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      system.debug('Email is sent--------------------->'+mail);
        }
    
    
}
 
}
}