Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
1 view
in Salesforce by (11.9k points)

I'm 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 would like to fill in the template data with the custom object record, "a", but I am getting this error:

WhatId is not available for sending emails to UserIds.

When I checked online, nowhere could I see that an Email in Apex can be only sent with a contact object in setTargetObjectId(). I don't want to temporarily create a contact just for sending an email!. What shall I do then?

Thanks in advance if someone has an idea

1 Answer

0 votes
by (26.7k points)

To actually perform this task, following code will help you to rectify the error. The only thing you need to do is you need to create a dummy contact of your own instead of going for an existing one. Following below is the dummy contact.

Contact dummyContact = new Contact();

dummyContact.LastName = 'DummmyContact';

dummyContact.Email = '[email protected]';

Once you created a dummy contact, then do this code.

// 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>{'[email protected]'});

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);

I hope this will help. Want to know more about salesforce apex? prefer this link batch apex in salesforce

Want to become a Salesforce Expert? join Salesforce certification now!!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...