So even with custom Apex methods it doesn't work. I get this error:
SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available with saveAsActivity for sending emails to Leads.: [whatId, 00Q17000008LRcA]
Which after researching leads me to this:
Don't use setWhatId in this case. You cannot specify a WhatId with a
Lead WhoId. Why? Because it's a limitation of the platform.
Fucking useless. Nevertheless, I found my solution:
Class:
public void send(Lead lead, String senderName, String replyTo) {
EmailTemplate emailTemplate = [SELECT Id, Name FROM EmailTemplate WHERE Name = 'VF template'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] { lead.Email };
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName(senderName);
mail.setReplyTo(replyTo);
mail.setTargetObjectId(lead.Id);
// mail.setWhatId(lead.Id);
mail.setTemplateId(emailTemplate.Id);
Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
inspectResults(results);
}
Controller:
public PageReference sendEmail() {
SendClass email = new SendClass();
email.send(lead, senderName, replyTo);
PageReference redirectPage = new PageReference('/' + lead.Id);
return redirectPage;
}
Page:
<div class="content">
<apex:form >
Sender name: <apex:inputText value="{!senderName}" id="senderName" /><br />
Reply-to address: <apex:inputText value="{!replyTo}" id="replyTo" /><br /><br />
<apex:commandButton value="Send" action="{!sendEmail}"/>
</apex:form>
</div>
You can just use params like I did with senderName
to get other information through.
whatId