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
SalesforceCrm AccountCRMSalesforceCrm AccountCRM 

SendEmail failed. REQUIRED_FIELD_MISSING, Missing targetObjectId with template:

Can any one help me out with this error .
1)When im using mail.setTargetObjectId(c.CustomerSignedId); the system start throwing a error .
SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template:
2)When im using mail.setTargetObjectId('00538000004bjrA'); the system start throwing a error .
SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, 8004B000000Qmzi]
3)When i use mail.setTargetObjectId(c.OwnerId); ,
i get the email but the merge fields value are not getting displayed
Here is the Code which im using the Batch Class :
global void execute(Database.BatchableContext bc, List < Contract > recs) {
        List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
        for (Contract c: recs) {
            if (c.Contact_Email__c != null) {
                List < String > toAddresses = new List < String > ();
                List < String > CcAddresses = new List < String > ();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                toAddresses.add(c.Contact_Email__c);
                ccAddresses.add(c.Account.Owner.Email);
               // toAddresses.add(c.Account.Owner.Manager.Email);
                mail.setToAddresses(toAddresses);
                mail.setCcAddresses(CcAddresses);
                mail.setTargetObjectId(c.CustomerSignedId);
                //mail.setTargetObjectId('00538000004bjrA');
                mail.setWhatId(c.Id);
                mail.setTemplateId('00X4B000000M3go');
                mail.setSaveAsActivity(false);
           mailList.add(mail);
            }
        }
        Messaging.sendEmail(mailList);
    }
Any Suggestion very much appreciated.
 
Best Answer chosen by SalesforceCrm AccountCRM
SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@Jyothsna   ,@sslodhi87 :Thanks for your response.
Please refer this link for the answer :
http://salesforce.stackexchange.com/questions/118857/sendemail-failed-required-field-missing-missing-targetobjectid-with-template/118876#118876

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi ,

If you are using a template you have to specify the Contact, Lead, or User to send the message to. This makes sure the template merges the correct fields in. You are not able to use the setToAddresses with a template, so the targetObjectId is how you set who the email goes to.
 
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(contactIds);
 mail.setTemplateID('00X90000001EWno');

Hope this helps you!
Best regards,
Jyothsna
SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@Jyothsna   :Thanks for your response.When i hardcode the value in
mail.setTargetObjectIds('003............');
i get the email ,but i dont want to hardcode the value for it.
When i use the
mail.setTargetObjectIds(c.contactIds);
the system throws an error
Invalid field contactId for SObject Contract
How do i get the contact id .When we are using the templateid ,we dont have an option to use the lookup fields as a merge field.
Any suggestion please.


 
sslodhi87sslodhi87

Hi

We will require the ContactID, LeadID or UserID to use the email template.

There is workaround if you don't have the contact Id.

You can use create one dummy contact and use the id of that contact. once email process completed you can delete the contact or use SavePoint and Rollback to rollback the contact record.

Also there is another way.. you can query the template body and set email body with template body.

Please let me know if you need futher information

Thanks,

JyothsnaJyothsna (Salesforce Developers) 
Hi,
 
// Pick a dummy Contact
Contact c = [select id, Email from Contact where email <> null limit 1];

Please check the below link for your requirement.

http://opfocus.com/sending-emails-in-salesforce-to-non-contacts-using-apex/

Hope this helps you!
Best Regards,
Jyothsna
SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@Jyothsna   ,@sslodhi87 :Thanks for your response.
Please refer this link for the answer :
http://salesforce.stackexchange.com/questions/118857/sendemail-failed-required-field-missing-missing-targetobjectid-with-template/118876#118876
This was selected as the best answer