function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dale WorthingtonDale Worthington 

Sending email with sforce.SingleEmailMessage() - Need to relate the message to a record

Hi All

Ive been playing with the sforce.SingleEmailMessage() ability in SFDC to send an email from a custom object and it works really well with the exception that i cannot see any ability to relate it back to the custom object as an activity.

At the moment the email relates to the contact (amc__Project__c.amc__ContactId__c), which i want it to, but i also want to relate it back to the record where the button is (amc__Project__c.Id). 
Within this documentation i cannot see any reference to linking back to an object which i can see when i click the 'Send an email' link and fill it out manually, ive put a screenshot below of the field im talking about.

Any help is appreciated

JS For the button

var message = new sforce.SingleEmailMessage(); 

message.replyTo = "{!User.Email}"; 
message.targetObjectId = "{!amc__Project__c.amc__ContactId__c}"; 
message.templateId = "00X5B000000LxrD"; 
message.saveAsActivity = true; 
var result = sforce.connection.sendEmail([message]);

if (result[0].getBoolean("success")) {
    alert("Email Sent");
  } else {
    alert( cResult[0].errors.message);
The field in the send an email button
User-added image