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
KD123456KD123456 

Attachment as part of notification email

Hi,

 

We have enabled email to case service. It works fine, the case is created and the notification emails are sent to the appropriate mailing groups properly. The emails sent by our customer will have attachments. As of now the attachments are created and I can view them in the Attachments related list. I am trying to make the attachments part of the notification email but am not able to get it done.

trigger notificationEmail on Case (after insert) {
    for(Case c : system.Trigger.new) {
        EmailTemplate template = new EmailTemplate();
        EmailMessage email = new EmailMessage();
        User owner = new User();
        Attachment attch = new Attachment();
        template = [select Id, Name, Body, Subject from EmailTemplate where Name = 'Support: Case Assignment Notification'];
        owner = [select Id, Name, Email from User where Id = : c.OwnerId];
        system.debug('the case id is' + c.Id);
        email = [select Id from EmailMessage where ParentId = : c.Id];
        attch = [select Id, Name, Body, ParentId, ContentType from Attachment where ParentId = : email.Id];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        if (attch.size() > 0) {
            Messaging.EmailFileAttachment emailAttch = new Messaging.EmailFileAttachment();
            emailAttch.setFileName(attch.Name);
            emailAttch.setBody(attch.Body);
            emailAttch.setContentType(attch.ContentType);
            emailAttch.setInline(false);
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {emailAttch});
        }
        string[] toadresses = new string[] {owner.Email};
        mail.setToAddresses(toadresses);
        mail.setReplyTo('spprtemail5@gmail.com');
        mail.setSenderDisplayName('SupportEmail');
        mail.setSubject(template.Subject);
        mail.setHtmlBody(template.Body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

 This code fails to retrieve information from the attachment or the Email message object for the given case Id and the case also fails to be created what is going wrong. How can this be fixed.

 

Thanks

KD