Here's my use case:
Existing logic: When an email with attachment is processed by email2case handler, case (Case A) gets created followed by email message and attachment resides with the email message. So, I have created before insert trigger on Attachment to change the parent id from EmailMessage to Case so that the attachment gets attached to Case.
New Logic: - When an email with attachment is processed by email2case handler, case (Case A) gets created followed by email message and I have after insert trigger on EmailMessage to create another Case (Case B), clone the EmailMessage and change its parent id to the new Case.
Now, I have original Case with its email message and new case with cloned email message
Because of the Attachment trigger mentioned in "Existing logic", attachment of the original email message gets attached to the original case but
I wanted it to attach with the new case (Case B)
Delete the original EmailMessage and its attachment(s) from the original case
Basically, moving the email message with its attachment(s) from original case (created by salesforce) to newly created case (created in emailmessage trigger)
Order of execution that I noticed:
- Case Triggers
- EmailMessage Triggers
- Attachment Triggers
Thanks for your suggestions!