You need to sign in to do that
Don't have an account?

Problem with trigger calling a visualforce email template
Hi!
I have problems with writing an email template for event-object. I have a trigger which should be fired after insert. It should send an email to the customer(account).
I am getting the following error msg when saving the event.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger sendEmailTemplate caused an unexpected exception, contact your administrator: sendEmailTemplate: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId : 00UT00000024Rod.: []: Trigger.sendEmailTemplate: line 26, column 12
The trigger follows here:
trigger sendEmailTemplate on Event (after insert) {
list<event> list_evt = new List<event>();
List<id> list_id = new List<Id>();
for (event e : Trigger.new) {
list_evt.add(e);
list_id.add(e.whoid);
}
List<Contact> conList = [select Id,Name,Email from Contact where Id in :list_id];
if (list_evt.get(0).Subject =='1.gang' ){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Contact con = list_evt.get(0).Who
mail.setSaveAsActivity(false);
mail.setTargetObjectId(list_evt.get(0).id);
mail.setWhatId(trigger.new[0].id);
mail.setTemplateId('00XA0000000a6IP');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
The VF template is as following(It's not finished, it's just a start for see that things works
<messaging:emailTemplate recipientType="Contact" relatedToType="Event" subject="Møtebekreftelse norKapital">
<messaging:htmlEmailBody >
<html>
<body>
<STYLE type="text/css">
TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1; text-align: center }
TD {font-size: 11px; font-face: verdana }
TABLE {border: solid #CCCCCC; border-width: 1}
TR {border: solid #CCCCCC; border-width: 1}
</STYLE>
<font face="arial" size="2">
Til {!recipient.Name}
Velkommen til møte hos oss den {!relatedTo.StartDateTime}.
</font>
</body>
</html>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
I hope someone can help me.
Hi
mail.setTargetObjectId(list_evt.get(0).id);
Here you are setting the targetobjectid to Event id. but the rule is You have to set the TargetObjectID to a Contact id, if you want to set WhatID.
so the line should be
mail.settargetObjectid(<Some Conatct id>);
the mail will go to the Email Field of the contact you are setting as TargetObject , and you can use the Merge fields of WhatId object
Hope it helps you. if it does accept the solution.plz
All Answers
Hi
mail.setTargetObjectId(list_evt.get(0).id);
Here you are setting the targetobjectid to Event id. but the rule is You have to set the TargetObjectID to a Contact id, if you want to set WhatID.
so the line should be
mail.settargetObjectid(<Some Conatct id>);
the mail will go to the Email Field of the contact you are setting as TargetObject , and you can use the Merge fields of WhatId object
Hope it helps you. if it does accept the solution.plz
Hi!
Tnx!! Now it worked!!!