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
sbajasbaja 

Visualforce email template Whatid issue

Hi Everyone,

 

I have created a visualforce email template where i need to send the meeting details to the lead.


<messaging:emailTemplate recipientType="Lead" relatedToType="Event" subject="abc" replyTo="abc@abc.com" >

 

I am using the trigger to send that mail , where i had written some code like :

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 mail.setSaveAsActivity(false);
mail.setToAddresses(toAddresses);
 mail.setCcAddresses(ccAddresses);
mail.setTargetObjectId(lead.Id);
mail.setWhatId(event.id);
mail.setTemplateId([select id from EmailTemplate where Name=: template].Id);
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });  

 

 

but I am getting exception "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.: "

 

 I had also tried to create a custom object and populating the event merge fields of template in that and tried to access that.

That is alos giving exception Invalid Field Reference.

 

Can you please tell me the work around that why it is not accepting event id.
Is there any other way to solve this issue.??

when I send test mail through template, it accept the id and merge all the fields. but giving exception when doing it through trigger.

 

Kindly Help me

 

Pradeep_NavatarPradeep_Navatar

The exception "INVALID_ID_FIELD" shows that setWhatId() method take only accounts, assets, campaigns, cases, contracts, opportunities, orders, products, solutions and custom objects as whatId. Since you are giving event.id in WhatId, it is giving such error. The correct code should be :  mail.setWhatId(Account.id);