Salesforce Apex SingleEmailMessage with Template to User not contact

0 votes

I am 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 !?

Mar 10, 2022 in SalesForce by surbhi
• 3,820 points
4,608 views

1 answer to this question.

0 votes
// Pick a dummy Contact Contact c = [select id, Email from Contact where email <> null limit 1]; // Construct the list of emails we want to send List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>(); Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage(); msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id ); msg.setWhatId( [select id from Account limit 1].id ); msg.setTargetObjectId(c.id); msg.setToAddresses(new List<String>{'random_address@opfocus.com'}); lstMsgs.add(msg); // Send the emails in a transaction, then roll it back Savepoint sp = Database.setSavepoint(); Messaging.sendEmail(lstMsgs); Database.rollback(sp); // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its // contents to a new SingleEmailMessage. Then send those new messages. List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>(); for (Messaging.SingleEmailMessage email : lstMsgs) { Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage(); emailToSend.setToAddresses(email.getToAddresses()); emailToSend.setPlainTextBody(email.getPlainTextBody()); emailToSend.setHTMLBody(email.getHTMLBody()); emailToSend.setSubject(email.getSubject()); lstMsgsToSend.add(emailToSend); } Messaging.sendEmail(lstMsgsToSend);

It actually works fine - with only the difference that i created a dummy contact of my own instead of using an existing one - because there might be none available or accidentally send out an email to the selected dummy - so better use the following:

Contact dummyContact = new Contact();
dummyContact.LastName = 'DummmyContact';
dummyContact.Email = 'dummmycontact@dummmycontact.com';

Hope this helps!

Join Salesforce Course today.

Thanks!

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

edited Jun 27, 2023 by Khan Sarfaraz

Related Questions In SalesForce

0 votes
0 answers

Salesforce Apex SingleEmailMessage with Template to User not contact

Im trying to send an email in ...READ MORE

Feb 28, 2022 in SalesForce by surbhi
• 3,820 points
3,061 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,098 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,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,093 views
+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,462 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,071 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
788 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 753 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,600 views
0 votes
1 answer
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