0

I'm using the standard Email2Case functionality, I'm also using an apex class that copies the email content into a case comment. What I'd like to add here is copying the email attachment into a case attachment.

public class EmailMessageCopyToCaseCommentsTrigger
{
    public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails)
    {
        List<CaseComment> comments = new List<CaseComment>();
        for (EmailMessage email:emails)
        {
            Id caseId = email.ParentId;
            CaseComment comment = new CaseComment(ParentId=caseId);
            comment.IsPublished = true;
            String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
            header += 'To: '+ email.ToAddress + '\n';
            header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
            if (email.TextBody!=null) {
                comment.CommentBody = header + email.TextBody;
            } else if (email.HtmlBody!=null) {
                comment.CommentBody = header + email.HtmlBody.replaceAll('\\<.*?>','');
            }

            comments.add(comment);
        }

        if (!comments.isEmpty())
        {
            insert comments;
        }

    }

    public static testMethod void testCopyEmailMessagesToCaseComments()
    {
        Case c1 = new Case(Subject='blah');
        insert c1;

        List<EmailMessage> emails = new List<EmailMessage>();
        emails.add(new EmailMessage(ParentId=c1.Id,FromAddress='yo@yo.com',FromName='Yo',Subject='Subject',TextBody='TextBody',ToAddress='to@to.com'));
        emails.add(new EmailMessage(ParentId=c1.Id,FromAddress='yo@yo.com',FromName='Yo',Subject='Subject',HtmlBody='<b>HtmlBody</b><i>more</i>',ToAddress='to@to.com'));
        insert emails;

        List<CaseComment> comments = [select Id,CommentBody from CaseComment where ParentId=:c1.Id];
        System.assert(comments.size()==2);

        for (CaseComment comment:comments) {
            System.debug(comment.CommentBody);
            System.assert(comment.CommentBody!=null && comment.CommentBody!='');
        }
    }
}
1
  • did anyone has a solution for this?
    – Itay B
    Jun 24 '13 at 18:11
2

Here's a basic modification that may be close to what you're looking for. This code is untested, but should be close to what you're looking for. Note that you may also want to try to apply logic to detect if the attachment is simply a image signature (e.g. by checking the HTML body of the email for a matching file name).

public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails)
{
    List<CaseComment> comments = new List<CaseComment>();
    // Store attachments
    Attachment[] attachments = new Attachment[0];

    for (EmailMessage email:emails)
    {
        Id caseId = email.ParentId;
        CaseComment comment = new CaseComment(ParentId=caseId);
        comment.IsPublished = true;
        String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
        header += 'To: '+ email.ToAddress + '\n';
        header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
        if (email.TextBody!=null) {
            comment.CommentBody = header + email.TextBody;
        } else if (email.HtmlBody!=null) {
            comment.CommentBody = header + email.HtmlBody.replaceAll('\\<.*?>','');
        }

        comments.add(comment);
        // Process any text attachments, such as JS files, HTML files, etc.
        if(email.TextAttachments != null) {
            for(InboundEmail.TextAttachment attachment: email.TextAttachments) {
                attachments.add(new Attachment(ParentId=caseId, Body=Blob.valueOf(attachment.body), Name=attachment.fileName, ContentType=attachment.mimeTypeSubType));
            }
        }
        // Process videos, images, etc.
        if(email.BinaryAttachments != null) {
            for(InboundEmail.BinaryAttachment attachment: email.BinaryAttachments) {
                attachments.add(new Attachment(ParentId=caseId, Body=attachment.body, name=attachment.fileName, ContentType=attachment.mimeTypeSubType));
            }
        }
    }

    insert comments;
    insert attachments;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.