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.
EmailMessage
is finished. You'll need a trigger on Attachment that understands parentId being of sobjecttype EmailMessage – cropredy Nov 3 '15 at 23:15