I need to send an email:
The code would look something like this:
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setToAddresses(new String[] { '[email protected]' });
msg.setSaveAsActivity(false);
// this causes a runtime error. Lead ID not allowed
msg.setWhatId(lead.Id);
msg.setTemplateId(templateID);
messages.add(msg);
Problem is, you cannot use a Lead ID for the WhatID. I'm not sending an email to the Lead, so I can't use the Lead for the TargetObjectId.
Is this possible?
Frustrating that SFDC still doesn't support a more flexible templating context, isn't it? Given your constraints, I think you do have options:
I've played with all of these approaches but the only one I've seriously implemented is #3. Option 1 seems like an attractive option assuming you can make it work.