Salesforce Apex SingleEmailMessage with Template to User not contact

0 votes

Im trying to send an email in Apex with the SingleEmailMessage() function using an existing Template and connecting it with a custom object record.

mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00Xb0000000iwks');
mail.setTargetObjectId(a.CAccount__r.OwnerId);  //lookup on account
mail.setToAddresses(new List<String>{a.CAccount__r.Owner.Email}); //email from account owner
mail.setTreatTargetObjectAsRecipient(false);
mail.setSaveAsActivity(false);
mail.setWhatId(a.Id);                        
this.mails.add(mail);    

Here i want to fill the template data with the custom object record "a". But i get the following error:

WhatId is not available for sending emails to UserIds.

Nowhere could i find an explicit answer that an Email in Apex can only be sent with a contact object in setTargetObjectId(). What i want to refrain from doing is to temporarily create a contact for the sole purpose of sending an email !?

Feb 28, 2022 in SalesForce by surbhi
• 3,820 points
3,059 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In SalesForce

0 votes
1 answer

Salesforce Apex SingleEmailMessage with Template to User not contact

// Pick a dummy Contact Contact c ...READ MORE

answered Mar 17, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 27, 2023 by Khan Sarfaraz 4,606 views
0 votes
1 answer

Salesforce Apex/JSON serialization with change on variable name

Apex does not support annotation for serialization. But, ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
4,094 views
0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

Because some fields in Apex Salesforce are ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
3,585 views
0 votes
1 answer

How to add days to date time in Salesforce Apex?

Beware the DST issue! The "addDays" function ...READ MORE

answered Mar 15, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 19, 2023 by Khan Sarfaraz 7,588 views
0 votes
0 answers

How to Write the Test class for Salesforce Apex Aura Enabled class?

stuck in here to write a test ...READ MORE

Mar 15, 2022 in SalesForce by surbhi
• 3,820 points
2,088 views
0 votes
1 answer
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,459 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,069 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
785 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 747 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP