1

I have the following Apex Trigger on the EmailMessage object (cases to email):

trigger EmailMessageTrigger on EmailMessage (after insert) {
    List<CaseComment> CaseComments = new List<CaseComment>();
    List<Attachment> Attachments = new List<Attachment>();

    for (EmailMessage e : Trigger.New) {
        CaseComments.add(new CaseComment(CommentBody=e.FromName +' has added an email to the case: \n https://gnerb-dev-ed.my.salesforce.com/'+e.Id,
                             ParentId=e.parentId));

        for (attachment a : e.attachments) {
            Attachment newFile = a.clone();
            newfile.ParentId = e.ParentId;
            Attachments.add(newFile);
        }
    }

    insert CaseComments;
    insert Attachments;
}

I need the email attachments to be cloned and attached to the email's parent case. I used the code from:

Copying attachments from object to object trigger

As an example and it doesn't seem to work. No errors are thrown or recorded to the log. I also tried putting the insert lines into a try/catch block to grab errors and no errors were produced. The case comment is added as desired.

EDIT:

Adding:

System.debug(e.attachments.size());

Outputs: 0

I tried to modify the for loop to the following:

    for (EmailMessage e : [SELECT Id, FromName, ParentId, (SELECT Id, ParentId FROM Attachments)
    FROM EmailMessage WHERE Id in :Trigger.New]) {

Unfortunately, it still shows 0.

2
  • 1
    First of all, the attachments (or any sobject) related list won't be in the scope of the trigger.new - they have to be queried for as you noted in your edit. But more importantly, SFDC can't insert the Attachment until the after insert of EmailMessage is finished. You'll need a trigger on Attachment that understands parentId being of sobjecttype EmailMessage
    – cropredy
    Nov 3, 2015 at 23:15
  • That makes sense; it's pretty much the exact opposite of what I wanted to do though. It just adds another piece to the puzzle. If that's the only option it's easy enough.
    – gNerb
    Nov 4, 2015 at 0:29

1 Answer 1

3

The following answer is based on crop1645's comment to the question:

trigger CaseAttachment on Attachment (after insert) {
    Set<Id> Parents = new Set<Id>();
    List<Attachment> newFiles = new List<Attachment>();

    for (attachment a : Trigger.New) {
        Parents.add(a.parentId);
    }

    for (EmailMessage e : [SELECT Id, ParentId FROM EmailMessage WHERE Id in :Parents]) { //loop through unique parents
        for (Attachment a : Trigger.New) {
            if (e.Id == a.ParentId) {
                Attachment newFile = a.clone();
                newFile.ParentId = e.ParentId;
                newFiles.add(newFile);
            }
        }
    }

    Insert newFiles;
}

This appears to be working in my tests.

2
  • Did this work for you ? I have a similar requirement and my trigger/handler is not getting invoked when attachments are created via an email. There is an active platform issue logged for this : Trigger on Attachment does not fire when attachment is created via Email
    – Jarvis
    Aug 16, 2018 at 13:58
  • Yes this worked for me but this was 3 years ago. It's entirely possible that the issue has been reintroduced. I no longer work for the company that I wrote this code for so I cannot tell you if it is working at present.
    – gNerb
    Aug 16, 2018 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .