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
ErrorProneErrorProne 

targetObjectId is optional?

the documentation indicates this field is optional, however when i omit it and use toAddresses and WhatId instead I get an error that indicates targetObjectId is required?

heres the code:

var message = new sforce.SingleEmailMessage();
        message.replyTo = "myemail@myemail.com";
        message.toAddresses = email;
        message.whatId= comment_id;
        message.templateId = "00XG0000001M0X7";
        var result = sforce.connection.sendEmail([message]);
        console.log(result);

James LoghryJames Loghry
Once you introduce a templateId, then the targetObjectId is required.  It's not called out very well, in my opinion, but this document hints at the requiredness: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
AshlekhAshlekh
Hi,

Yes According to docuementation. 

Targetobject id is Optional. The object ID of the contact, lead, or user the email will be sent to. The object ID you enter sets the context and ensures that merge fields in the template contain the correct data
Do not enter the object IDs of records that have the Email Opt Out option selected.

All emails must have a recipient value in at least one of the following:
toAddresses
ccAddresses
bccAddresses
targetObjectId
targetObjectIds

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail.htm



IF it helps you than please mark it as a solution and ENJOY APEX

AshwaniAshwani
Target object ID is required when you set template for email.

If you set "toAddress" and didn't set "templateID" then targetObjectId is optional.

If you set "templateID" then targetObjectId is required.