+ Start a Discussion

How to setwhatid for events



I'm working on a trigger that should send an email to a contact when an event is created on the salesforce calendar.

We are using a VF email template which is related to events. The problem is that we would like to send information related to the event in the email to the contact.


If we try "email.setwhatId(event.Id);"


We get the following error:

"First exception on row 0; first error: INVALID_ID_FIELD, Only accounts, assets, campaigns, cases, contracts, opportunities, orders, products, solutions and custom objects are allowed as whatId.: "


What is the right way to do this? The whole purpose of this trigger is to notify the contact with the details of the agreed meeting after setting it up.







You are getting this error because whatid deals with object things. I would suggest that create a custom template in which you have to use the event field’s value through the merge field. So when you have finished with the custom template pass that id inside the setTargetObjectId methods of SingleEmailMessage. Try the below code as reference:


SingleEmailMessage message = new SingleEmailMessage();
message.setTargetObjectId(‘00X90000000em3G’)// Please pass your template id here which you have created for the event.


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 


I was hoping to avoid creating a custom object, is there a way to achieve this without creating the new object?





You don’t have to create the object you have to simply create the email template and pass that id inside the above mention method. You can create the templae by going through this path:

Yourname-> setup->PersonalSetUp->Email->MyTemplate


Hi there,


I'm having the same problem with sending an email notification to a contact/lead when an event is created.


Normally I would use the Messaging.setTargetObjectId property to set the recipient of the mail (contact/lead), the setTemplateId property to set the templateand the setwhatId property to set the record used for the merge fields (event). Unfortunately the setwhatId property doesn't support the Event object.


If I use the setTargetObjectId property to set my VF template, how can I set the recipient and the relevant record for the merge fields?