9

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.

11

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".

0
3

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.

E.g.

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
 emails.add(email);
}

Messaging.SendEmail(emails); 
//will create as many activities as there are emails, and insert all of them individually.
3
  • 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
    Oct 7 '15 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
    Oct 7 '15 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
    Oct 7 '15 at 19:13

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.