+ Start a Discussion
aditya prasadaditya prasad 

Problem in after insert

I want to insert attachments in attachment related list of Case whenever I send an outbound message with an attachment. Same attachment should be available in Attachment related list.

I created following trigger fi=or this.
trigger emailTrigger on EmailMessage (after insert)
for(EmailMessage em: Trigger.new)
    {
     emailmsgId = em.Id;
     
    }
    system.debug('setr'+emailmsgId);
    AttList = [select Id, ParentID,Name, body,ContentType,BodyLength from Attachment where ParentID =: emailmsgId];


I am getting correct Id when i check in debug. Bur i don't get any value for AttList. always return row 0. If I hardcode any Outbound message Id, I get the attachments associated with it if any.
Where I am doing the mistake any idea?

Thanks for your help.
Mahesh DMahesh D
trigger emailTrigger on EmailMessage (after insert)
Set<Id> emailmsgId = new Set<Id>();
for(EmailMessage em: Trigger.new)
    {
     emailmsgId.add(em.Id);     
    }
    system.debug('setr'+emailmsgId);
    AttList = [select Id, ParentID,Name, body,ContentType,BodyLength from Attachment where ParentID IN: emailmsgId];

You can try this code.

Regards,
Mahesh
Richard Jimenez 9Richard Jimenez 9
Hi,

I suspect the attachment record is inserted after the emailmessage is created (as it is the parent record). You will need to move your trigger to the attachment object (before insert) and re-parent the attachment to the case.

Another option, instead of reparenting the attachment, you could create a VisualForce component/page embedded in the case page layout that displays a list of attachments that are related to a emails from the case. This would then retain the attachments against the emails for auditability.

Thanks,
Richard.