[SalesForce] WhatId Is Not Available For Sending Emails To UserIds

I am getting the error – WhatId Is Not Available For Sending Emails To UserIds while trying to send email to user from an Apex Trigger using SingleMailMessage.

The code is working absolutely fine with a predefined standard EMail Template but the only problem is – it is unable to produce values of Merge Fields from the Custom Object that I wanted to set as whatId.

Any idea how to get rid of this error? As an alternative I might need to set the htmlBody of the mail in code but I am not sure.in that case how I can use the background layout that (it has some custom pink color and images set with company logo in the mail background)I had set in my email template?

   List<Messaging.SingleEmailMessage> eMail = new List<Messaging.SingleEmailMessage>(); 
   for (Event__c ea : eaList)
   {                   
    Set<String> invitedBy= new Set<String>();
    List<String> sendTo = new List<String>();  
    Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

    invitedBy.add(ea.CreatedBy.Email);       // to capture unique values
    sendTo.addAll(invitedBy);
    singleMail.setToAddresses(sendTo);

    singleMail.setReplyTo('xxxx@gmail.com');
    singleMail.setSenderDisplayName('xxxx');
    singleMail.setTargetObjectId(ea.CreatedById);

    //set template Id
    singleMail.setTemplateId(et.Id);
    singleMail.setSaveAsActivity(false);
    // singleMail.setWhatId(ea.Parent_event_c);  // gives error WhatId Is Not Available For Sending Emails To UserIds
    eMail.add(singleMail);
}
Messaging.sendEmail(eMail);

I tried to follow the link – whatId when using custom object
and changed the code…but here I ended up doing DML (inserting contact) within for loop..any idea how to avoid this ?

 List<Messaging.SingleEmailMessage> eMail = new List<Messaging.SingleEmailMessage>(); 
   for (Event__c ea : eaList)
   {                   
    Set<String> invitedBy= new Set<String>();
    List<String> sendTo = new List<String>();  
    Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

 // insert the mail id as a contact as suggested in the link 
tc = new Contact(email = ea.CreatedBy.Email, firstName = ea.CreatedBy.Name ,lastName = ea.CreatedBy.Name);
    insert tc ;
    invitedBy.add(tc.Email);       // to capture unique values
    sendTo.addAll(invitedBy);
    singleMail.setToAddresses(sendTo);

    singleMail.setReplyTo('xxxx@gmail.com');
    singleMail.setSenderDisplayName('xxxx');
    singleMail.setTargetObjectId(ea.CreatedById);

    //set template Id
    singleMail.setTemplateId(et.Id);
    singleMail.setSaveAsActivity(false);
    // singleMail.setWhatId(ea.Parent_event_c);  // gives error WhatId Is Not Available For Sending Emails To UserIds
    eMail.add(singleMail);
}
Messaging.sendEmail(eMail);
//delete the contact once email sent
delete tc;

Best Answer

Per the documentation:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data

So, since the targetObjectID field is not a contact, you cannot specify a WhatID.

There are some workarounds using a VF email template:

http://www.forcedisturbances.com/2012/04/automatically-sending-email-reminders.html

Or, you could on the fly create a contact for the user, use that, then delete the contact.

whatId when using custom object