+ Start a Discussion
Puneet KhoslaPuneet Khosla 

treatTargetObjectAsRecipient not saving Activity History

I am able to send emails from Apex code, however it is only saved in Activity history of Case (object in setWhatId) if I set the TargetObjectId.

My requirement is that I might / might not want to send the email to TargetObjectId, so I have made use of ToAddress.
This however doesn't save the Activity history (or basically create a task as completed + save email message) & if I also include TargetObjectId, it will save the email as Activity history, however I donot want to include TargetObjectId.

In Winter16 there is a feature treatTargetObjectAsRecipient which when set to false won't send the email to TargetObjectId (which is what I wanted), but it is also not saving the Activity History.

So is there a way, where I can Save the email in the Activity History + provide ability to avoid email being sent to the TargetObjectId, but send it to other email addresses mentioned on my page in ToAddress field (which may or may not contain the contact being used as TargetObjectId).

I don't want to create a Contact , send Email and then delete contact.
I was able to pretty easily reproduce this (code below) and to me it seems like it's a bug.  If you can, I'd reach out to Salesforce support and provide the simple reproducer below and get their oppinion.  As for a workaround, you can always create the Activity yourself and insert it on the Case.
Id caseId = 'xxxx';
Id contactId = 'yyyy';
Id templateId = 'zzzz';
String emailAddress = 'contact@example.com';

Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setToAddresses(new List<String>{emailAddress});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
Drew ChaplinDrew Chaplin
I'm having the same trouble.   arg...