+ Start a Discussion

Email to Case Comments - add attachments



I'm using the standard email-to-case functionality to attach emails to new and existing cases. I added the code below to "convert" emails into case comments.

I also want to add the email attachment as a case attachment. How can this be added to the code?


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 += 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('\\<.*?>','');
        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];
        for (CaseComment comment:comments) {
            System.assert(comment.CommentBody!=null && comment.CommentBody!='');


Anthony PicaAnthony Pica

You may want to check out Email to Case Premium on the AppExchange. It might save you a bunch of time. This screenshot shows what you're looking for: https://appexchange.salesforce.com/servlet/servlet.FileDownload?file=00P3000000H8SE6EAN


Here's the listing: https://appexchange.salesforce.com/listingDetail?listingId=a0N30000001R5cyEAC


Disclaimer: I work for the org that makes E2CP

Marko LamotMarko Lamot



You would need to do that directly on attachment trigger:

- check if attachment belongs to emailmessage

- if belongs to emailmessage, change parent to emailmessage parent (which is case)


Below is one example (please note, that you would need to bulkify the example code in order to avoid DML error on mass email insert):



trigger emailAttachment2Case on Attachment (before insert) {
    for( Attachment a : trigger.new ) {  
       if( a.parentid != null )
{ String s = a.parentid; if( s.substring( 0, 3 ) == '02s' ) // 02s is for emailmessage
{ a.parentid = [select parentID from EmailMessage where id = :a.parentid].parentID; // bulkify this line !!! }
} } }


Thanks Marko! is there a way to include this code in my class (instead of creating another trigger)?

Marko LamotMarko Lamot



if you want automaticaly - as mail comes into SF - do this, than it must be a trigger on attachment, that implements the functionallity - OR calls another class where this functionality is implemented.

Like - I assume - you have a trigger on mail for email.