Salesforce supports sending Email under Activity History on Opportunities/Accounts etc.

Where is this information stored?

Is there a way to send, query and store email messages via the API? The EmailMessage Object documentation suggests it should be possible on the SOAP API.

2 Answers 2


Firstly, this is not writeable, except by the system as the result of an activity happening.

So when you generate email through some "natural" means, and it stores an activity, the system writes to an object called Activity History. This object has absolutely no direct access.

So this query is illegal:

select Id from ActivityHistory

It returns the error:

entity type ActivityHistory does not support query

But, you can get query access to it as the child relationship of a primary object. For instance, I could do this query:

Select Name, (Select Subject,ActivityType from ActivityHistories) from Account

This you could do with standard SOAP API functionality.

Writing to it directly still remains impossible. Still, any call you do to the sendEmail functionality should be recorded in an object's Activity history, which is, for all intents and purposes "writing to it".


Important to note however is the fact that when using Messaging.SingleEmailMessage if you leave the setActivityAs property set to true - the default, that creates the record in activity history you mentioned - and send a list of emails, it inserts the activities for those emails individually instead of in bulk, so depending on how your triggers are set up, this could be problematic.


List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); 
//Apparently Stack Exchange is refusing to display what I actually wrote here. Creating a new list of Messaging single Email Message Type.

for(Id targetObjectToEmail :listofTargetObjectIdsEmailing){
 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMesssage();
 email.setTargetObjectId = targetObjectToEmail;
 email.setSubject = 'test email';
 email.setActivityAs(true); <---default, but displayed here for clarity

//will create as many activities as there are emails, and insert all of them individually.
  • When you paste code into a post, select it, then click on the pre-formatted text icon {} which moves it over by four spaces so it will display properly. That's why your code wasn't displaying as you wanted it to. Not certain if you were trying to create a link to pastie or not, but included it in your code.
    – crmprogdev
    Commented Oct 7, 2015 at 18:15
  • Thank you! I was trying to include the pastie link if all else failed to better display the code, but forgot to delete it when I decided to go with the comment approach instead. :)
    – KB145
    Commented Oct 7, 2015 at 19:00
  • I've removed the pastie link for you. You can always edit your own post at any time by clicking on the edit link at the bottom left.
    – crmprogdev
    Commented Oct 7, 2015 at 19:13

You must log in to answer this question.

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