+ Start a Discussion

Need help in debugging the trigger to update the parentId of 'Attachment'

Hi all,


I'm using the following trigger to change the parentId of a Case Email Attachment (outbound email message) from EmailMessageId to CaseId so that the attachment will be automatically saved under the Case Attachments related list.


trigger aaa_Case_PopulateAttachments on EmailMessage (after insert) {
    List<Id> eMsgIds = new List<Id>();
    for(EmailMessage msg:trigger.new){
            System.debug('Email Message: '+msg.Id);

    List<Attachment> attachList = [Select Id,ParentId from Attachment where ParentId in :eMsgIds];
    System.debug('List of Attachments:' + attachList); //This is returning 0 rows!!

    Map<Id,EmailMessage> eMsgMap = new Map<Id,EmailMessage>([Select ParentId from EmailMessage where Id in :eMsgIds]);
    System.debug('Map of Email Messages:' + eMsgMap);

    List<Attachment> attachUpdateList = new List<Attachment>();

    for(Attachment att:attachList){
            System.debug('Attachment Parent Email Message : '+att.ParentId);
        update attachUpdateList;


If I click on the Case Email Attachment after the email is sent, it does show a recordid for that attachment. I hope that the attachment is saved somewhere in SFDC as a record of some object (because it has a recordid). But I can't figure out where it is being saved!


I've used a trigger on 'Attachment' to find if the Email Attachments are saved under 'Attachments' or not. The trigger is executed for the attachments created directly under Case record but not for those created under Case Email!!


trigger aaa_Attachment on Attachment (after insert) {
    for(Attachment a:trigger.new){
        System.debug('Attachment Name: '+a.Name+'Attachment Id: '+a.Id+'\nRelated To: '+a.ParentId);


Can anyone please help me out? Thanks.


I ran into this same issue a while back and what I found was this.  If you send an email to a Contact/Lead through the Salesforce UI and you add an attachment to that email, the attachment is not stored in Salesforce.  


There has been an idea posted about this so please vote it up - https://sites.secure.force.com/success/ideaview?id=08730000000BpSJAA0


~ Clint