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
DannyK89DannyK89 

Email Templates

I am having trouble using an email template. The error I get is: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [] 

 

Here is the code that I use to send the eamil:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{to};
            String[] bbcAddress = new String[]{bcc};
            if(attachlist != null){
            list<Messaging.Emailfileattachment> emailattachlist = new List<Messaging.Emailfileattachment>();
                for(Attachment a : attachlist){
                    Messaging.Emailfileattachment emailattach = new Messaging.Emailfileattachment();
                    emailattach.setFileName(a.Name);
                    emailattach.setBody(a.Body);
                    emailattachlist.add(emailattach);
                }
                mail.setFileAttachments(emailattachlist);
            }
            mail.setToAddresses(toAddresses);
            mail.setBccAddresses(bbcAddress);
            if(tempid == null){
                mail.setPlainTextBody(body);
                mail.setHtmlBody(body);
                mail.setSubject(sub);
            }
            else{
                mail.setTemplateId(tempid);
                mail.setWhatId(candmap.get(cand).Id);
                mail.setTargetObjectId(UserInfo.getUserId());
            }
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can not set a userId in setwhatId in mail. 

 

WhatId : 

Optional. If you specify a contact for the targetObjectId field, you can specify awhatId as well. This helps to further ensure that merge fields in the template contain the correct data. The value must be one of the following types:
  • Account
  • Asset
  • Campaign
  • Case
  • Contract
  • Opportunity
  • Order
  • Product
  • Solution
  • Custom

All Answers

Shashikant SharmaShashikant Sharma

You can not set a userId in setwhatId in mail. 

 

WhatId : 

Optional. If you specify a contact for the targetObjectId field, you can specify awhatId as well. This helps to further ensure that merge fields in the template contain the correct data. The value must be one of the following types:
  • Account
  • Asset
  • Campaign
  • Case
  • Contract
  • Opportunity
  • Order
  • Product
  • Solution
  • Custom
This was selected as the best answer
HoneyHoney

I am using following template which will slecet by trigger, but when mail recieved thenfields that i want to show with relatedto type not come in mail.... please help me thanks in advance.......... all fields present in Event.

 

<messaging:emailTemplate subject="cbt" recipientType="User" relatedToType="Event">


<messaging:plainTextEmailBody >
Hello,

{!$User.FirstName} {!$User.LastName}  committed to call  {!relatedTo.Contact_Name__c} at {!relatedTo.StartDateTime}.
This notification has been sent 15 minutes prior to the scheduled time.

The case can be accessed via the following link: https://cs4.salesforce.com/{!relatedTo.Case_Id__c}
Case Subject: {!relatedTo.Case_Subject__c}

Thank you,
SalesForce

</messaging:plainTextEmailBody>
</messaging:emailTemplate>