You need to sign in to do that
Don't have an account?
Sarah 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.
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!
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!
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!
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.