I'm leveraging 'Send an Email' button for a custom object(comm__c
) which has a lookup to the Case
object. The users sends email using this button via Related List on the case page layout.
I'm creating a comm__c
record every time an outbound email is sent by writing a trigger on EmailMessage
object. I would also like to show attachments added on the email underneath the comm__c
record.
For this I wrote the below trigger logic on the Attachment
object –
public static void cloneOutboundEmailAttachments(List<Attachment> newList){
Set<Id> Parents = new Set<Id>();
List<Attachment> newFiles = new List<Attachment>();
for (attachment a : newList) {
if(a.ParentId.getSObjectType() == EmailMessage.getSObjectType())
Parents.add(a.parentId);
}
SYSTEM.DEBUG('EmailMessagesSIZE'+Parents.size());
if(!parents.isEmpty()){
Map<Id,EmailMessage> emailMap = new Map<ID, EmailMessage>([SELECT RelatedToId FROM EmailMessage WHERE Id in :Parents]);
for (Attachment att : newList) {
if (att.ParentId.getSObjectType() == EmailMessage.SObjectType) {
SYSTEM.debug(att);
Attachment newFile = att.clone();
newFile.ParentId = emailMap.get(att.ParentId).RelatedToId;
newFiles.add(newFile);
}
}
SYSTEM.DEBUG('newFilesSIZE'+newFiles.size());
Insert newFiles;
}
}
To my dismay I found that there is a platform bug which doesn't invoke triggers on Attachment
object when user sends email via Related List and adds an attachment file – Trigger on Attachment does not fire when attachment is created via Email
How can I circumvent this issue?
Best Answer
Finally, I wrote a batch class which copies the email attachments to the custom object.