Skip to main content
Added extra information erroneously (unless this was used as a solution - the information is now available either way) provided as an answer.
Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

I am having an html email template which uses certain merge fields from a custom object. Now I am using following apex code to send certain emails to users.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setTargetObjectId(t.OwnerId); 
        mail.setTemplateId(et.Id);
        mail.setWhatId(t.Id); 
        mail.SaveAsActivity = false;
        Messaging.sendEmail(new Messaging.singleemailMessage[] {mail});

Code is saved without error but when I run it it says "can't use whatId when sending email to users". Can anyone explain me how to overcome this error? When I use a visualforce template then same code works fine.

I am having an html email template which uses certain merge fields from a custom object. Now I am using following apex code to send certain emails to users.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setTargetObjectId(t.OwnerId); 
        mail.setTemplateId(et.Id);
        mail.setWhatId(t.Id); 
        mail.SaveAsActivity = false;
        Messaging.sendEmail(new Messaging.singleemailMessage[] {mail});

Code is saved without error but when I run it it says "can't use whatId when sending email to users". Can anyone explain me how to overcome this error?

I am having an html email template which uses certain merge fields from a custom object. Now I am using following apex code to send certain emails to users.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setTargetObjectId(t.OwnerId); 
        mail.setTemplateId(et.Id);
        mail.setWhatId(t.Id); 
        mail.SaveAsActivity = false;
        Messaging.sendEmail(new Messaging.singleemailMessage[] {mail});

Code is saved without error but when I run it it says "can't use whatId when sending email to users". Can anyone explain me how to overcome this error? When I use a visualforce template then same code works fine.

Tweeted twitter.com/#!/StackSalesforce/status/252876479514415105
Source Link
doga
  • 4.6k
  • 11
  • 63
  • 94

whatId when using custom object

I am having an html email template which uses certain merge fields from a custom object. Now I am using following apex code to send certain emails to users.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setTargetObjectId(t.OwnerId); 
        mail.setTemplateId(et.Id);
        mail.setWhatId(t.Id); 
        mail.SaveAsActivity = false;
        Messaging.sendEmail(new Messaging.singleemailMessage[] {mail});

Code is saved without error but when I run it it says "can't use whatId when sending email to users". Can anyone explain me how to overcome this error?