function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
force_archforce_arch 

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){
        if(msg.HasAttachment){
            System.debug('Email Message: '+msg.Id);
            eMsgIds.add(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){
        if(eMsgMap.containsKey(att.ParentId)){
            att.ParentId=eMsgMap.get(att.ParentId).ParentId;
            System.debug('Attachment Parent Email Message : '+att.ParentId);
            attachUpdateList.add(att);
        }
    }
    if(attachUpdateList!=null&&!attachUpdateList.isEmpty()){
        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.

ClintLeeClintLee

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