function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Martha VMartha V 

INVALID_ID_FIELD, Invalid entity type for whatId.

I have an apex extension for a visualforce page that sends emails using the SignleEmailMessaging method. It sends email using an email template with merge fields from a custom object called ParticipantEmail. However, I am getting an error on the WhatId method call.

here is that part of the code:
       //the class WrapTemp has two attributes, one of type ParticipantEmail__c named tempRec and the second one is a string named type_member
        for (WrapTemp wCM : pEmails){
            Messaging.SingleEmailMessage mess = new Messaging.SingleEmailMessage();
            List<String> bccAddys = new List<String>();
            bccAddys.add(UserInfo.getUserId());
            mess.setBccAddresses(bccAddys);
            system.debug('wCM.tempRec.ContactId = ' + wCM.tempRec.ContactId__c);
            system.debug('wCM.tempRec.Id = '+wCM.tempRec.Id);
            //set the contact to be emailed
            mess.setTargetObjectId(wCM.tempRec.ContactId__c);
            system.debug('wCM.tempRec.Id = '+wCM.tempRec.Id);
            id theRecId = wCM.tempRec.Id;
            system.debug('theRecId = '+ theRecId);            
            mess.setWhatId(theRecId);
            if (wCM.type_member == 'Participant') 
                mess.setTemplateId(temp.Id);
            else 
                mess.setTemplateId(volTemp.Id);                 
            messages.add(mess);            
        }
        messaging.sendEmail(messages);

Here is what I get from the debugger:
User-added image
It says the id is a284B0000007BOiQAM

but then the error I get says this 


System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Invalid entity type for whatId.: [whatId, a284B0000007BPf]
Error is in expression '{!SendTheEmails}' in component <apex:commandButton> in page cmsendemailsvf: Class.CMSendEmails.SendTheEmails: line 129, column 1


Does anyone see what my problem is here?  I have no idea.
Andrea BarquinAndrea Barquin
To recover from this you need to change your Custom object by checking that you have the “Allow Activities” checkbox checked (on the standard or custom object) being used as base object in document action. Once this is checked, you should be able to send an email without error.
Anubhooti PaliwalAnubhooti Paliwal
Andrea Barquin, This helped me, but still merged field are not getting populated.
Idealist Consulting 26Idealist Consulting 26
Wow - I had this problem with Volunteers for Salesforce (V4S) managed package email templates and this was the perfect fix - quick and dirty, just how I like it!  THANK YOU VERY MUCH, Andrea Barquin!