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
dinesh.sfdclearner1.394530801200392E12dinesh.sfdclearner1.394530801200392E12 

Store all outgoing emails

Requirement:
Display an Emails related list under Contacts that displays all the emails sent to the Contact from Salesforce (from Email alerts, or Triggers, etc). 

Basically whenever an automated email is sent from Salesforce, the details of the email(From, To, Date, Subject, Body, Attachments) should be saved along with the contact as a related list for future referrence.

Can anyone please suggest a way to implement this functionality.

Thanks!
Sonam_SFDCSonam_SFDC
Hi Dinesh,

When ever an email is sent to contact/lead using trigger/automation, use the targetObjectId to specific the USer/Contact/Lead the email is to be attached to and it will automatically create an enter in the Activity related List as a completed Task :
This happens because of the setSaveAsActivity(Boolean) field which is true by default
Read more: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_base.htm#apex_Messaging_Email_setSaveAsActivity

dinesh.sfdclearner1.394530801200392E12dinesh.sfdclearner1.394530801200392E12
Hi Sonam,

Thanks for the response. I believe this will work when the email is sent from the trigger using the OutboundEmailHandler but how to capture the emails that are sent from automated workflow alerts that don't involve trigger or any other class?
Sonam_SFDCSonam_SFDC
aah, you are right - When using Workflow rules to trigger email alerts, activities are not automatically logged when the email is sent. 

To track email alerts, have the Workflow trigger a task (in addition to the email alert that is sent).
You can assign the task to the record owner, creator, a certain role, or a specific user. You can also set the task to "Completed" so it will just be used for tracking purposes. 

Once the Workflow is triggered, the email will be sent and a completed activity will be logged on that particular record simultaneously. 

Found this interesting workaround on: https://success.salesforce.com/answers?id=90630000000gvnQAAQ
dinesh.sfdclearner1.394530801200392E12dinesh.sfdclearner1.394530801200392E12
That's a good one. But I'm wondering if it is possible to capture the details of the email shuch as Sender, Recipient and Body of the email that has been sent. Can you please clarify? Thanks!