+ Start a Discussion
Sarah DickinsonSarah Dickinson 

EmailMessage record created when sending SingleEmailMessage

Hi,

Today I noticed that my Data Storage had gone way up, and the first object in the list was Email Messages. When I queried all the Email Messages in our instance I noticed that everytime we send an email in APEX, there is an EmailMessage record created.
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('TEST');
email.setToAddresses(new String[] {'youremail@mail.com'});
email.setPlainTextBody('TEST');
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email},false);

List<EmailMessage> emailMessage = [SELECT Id, Subject, TextBody FROM EmailMessage WHERE ParentId = null AND RelatedToId = null AND ActivityId = null AND MessageDate = TODAY AND Subject =:email.Subject];
System.assertEquals(1, emailMessage.size());

Is that documented somewhere? I feel like it has not always been the case.
Is there a way to not have those EmailMessage records created?

Thanks!
 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I think the default value of setSaveAsActivity(Boolean) is true so try setting it to false.
Sarah DickinsonSarah Dickinson
Hi,

It seems to work when using setSaveAsActivity(false).

Here is the documentation Salesforce has for that method on the developer site:
Optional. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates.

I am very confused about that, because to me Activity is very different from EmailMessage.
Also, in my example I am not using targetObjectId or targetObjectIds but it still behaves diffrently.

Thanks!

Pedro Freire (Merkle)Pedro Freire (Merkle)
Hi,

Maybe it's because you have enhanced email active. When enhanced email is active messages are no longer stored as Activitiy/Task but as EmailMessage.
Pedro Freire (Merkle)Pedro Freire (Merkle)
Check this Knowledge Article: Enhanced email and the Email Message object (https://help.salesforce.com/articleView?id=000239922&type=1" target="_self). It says "When Enhanced Email is activated, emails sent from Salesforce are logged as Email Messages and Tasks." and then "The Tasks are not displayed on records in Salesforce with Enhanced Email activated."