0

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!

3
  • (1) I presume you want to reparent EmailMessage even if the incoming email has no attachments? (2) Does the reparenting have to happen immediately or can it be done in a scheduled batch job?
    – cropredy
    Commented Nov 20, 2014 at 4:57
  • Reparent EmailMessage and also its attachment(s) if any to the Case that gets created by the trigger on EmailMessage. It would be better if it can happen immediately otherwise let me know details about using a job. Commented Nov 20, 2014 at 5:47
  • Any updates on this? Commented Nov 23, 2014 at 1:29

1 Answer 1

2

Sethu

I did think about this and here is a condensed answer

Given that an incoming EmailMessage may not have any Attachments, then a trigger on Attachment won't work as it would never fire if an attachment-less EmailMessage arrived.

A trigger on EmailMessage wouldn't work as it would miss the attachments added after the EmailMessage trigger executed

So, the general approach to take is for the EmailMessage trigger to create the clone Case and:

  • include in the cloned Case the ID of the original Case
  • a boolean flag that says is_emailMsg_reparenting_required__c

Then, you can use your favorite async pattern to soql query all the cloned Cases waiting the email reparenting and then do the reparenting through APEX DML, resetting the boolean flag when done

I'd look first at @future but I'm not sure you can guarantee that the future will always run after the Attachments are inserted as the transaction context of EmailToCase is not known to me without more research

Other options would be an apex class implementing the new queueable interface, a scheduled apex class that runs regularly, or the Dan Appleman Async Apex Design Pattern which is quite elegant and can run every few seconds handling all your async needs, not just this particular use case.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .