3

When using auto-responders for email-to-case, the outbound emails are attached to the case. I'd like to perform the same action, when I am sending outbound emails via my own inbound email handler.

In my specific usage case (my custom inbound email handler), I am setting the 'targetObjectId' to a dummy contact (as a contact is required for emailing templates) and the 'WhatId' to the case id. There is no record of my email being sent out (though they are being sent, as can be seen when testing 'live').

I do have experience with creating an EmailMessage object and attaching it to a case (via parentId). I would think that an outbound message would 'magically' attach itself to a given object, but there is no method for specifying it as such.

Any help would be appreciated. Thanks!

4

Yep, when you are assembling the message, use:

message.setSaveAsActivity(true)

which will capture it as a Completed Task on the Case.

1

Messaging.SingleEmailMessage is a class that Salesforce uses to pass data to its email services in order to physically send an email to its target email address(es). EmailMessage is the database representation of said email transaction to be stored in Salesforce. Now, for out-of-the-box features like Email2Case and auto-responders, Salesforce automatically creates an EmailMessage record when an inbound/outbound email transaction occurs. However, if you're creating your own email processing via Apex, Salesforce does not give you an EmailMessage record when you send an email.

With all that being said, the easiest thing to do transpose the data from your SingleEmailMessage object to an EmailMessage record, and insert it to the database. If I'm not mistaken, most if not all of the API field names between the object types match. Something along the lines of:

EmailMessage emToInsert = new EmailMessage();
emToInsert.toAddress = DummyContact.email;
emToInsert.Subject = singleEMToSend.Subject;
emToInsert.HTMLBody = singleEMToSend.HtmlBody;
//additional fields to map over
//....
emToInsert.ParentId = singleEMToSend.WhatId;
insert emToInsert;
0

I would think that an outbound message would 'magically' attach itself to a given object, but there is no method for specifying it as such.

Very little happens by "magic" alone when programming in Apex. In your inbound email handler, you'd do something like the following to create the attachment from an inbound message:

if(email.binaryAttachments != null && email.binaryAttachments.size() > 0){
   for(integer i=0;i<email.binaryAttachments.size();i++{
     Attachment attachment = new Attachment();
     // typically you'd attach this to a new contact record you'd created in the inboundmailhandler or one that you'd verified
     attachment.ParentID = contact.Id;
     attachment.Name email.binaryAttachment[i].filename;
     attachment.Body = email.binaryAttachments[i].body;
     insert attachment;
   }
}

To later add your outbound attachment when creating your outbound email, add lines similar to following: (note: am using pseudo-code here)

email.binaryAttachments.filename = [query for the attachment containing the email.binaryAttachments[i].body' saved above. This assumes there were no other file attachments that were saved, otherwise, you'd be re-sending any other attachments too];
outbound.setFileAttachments = FileAttachments;

This should point you in the correct path toward finding the solution that will satisfy your needs. You may want to take a look at the SF Object API Reference which should give you all the calls for email messaging objects.

-1

Use SaveAsActivity(True). Btw, it is true by default.

1
  • This is very similar to an answer that was given 5 years ago. The method/property name appears to be wrong, and it'd be good to include a source to corroborate your claim of "true by default". Similar answers can be fine, but there should be something to distinguish them (updates now that new features/documentation are out there, a different approach, etc...). Linking to documentation, and providing a simple example would improve this answer. For now, I've downvoted. Feel free to @ me in a comment if you come back and edit this answer so I can reconsider my downvote. – Derek F Nov 9 '19 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.