You need to sign in to do that
Don't have an account?
Raghavendra A
Visual force Email template Merge fields issue
Hi All,
I am working on a trigger that sends out an email everytime a Opprotunity record is created. I am using Visualforce email template and setting the TemplateId in the trigger code. But when I do this the merge fields are not Populated from the Opportunity record. Entire content of the Visualforce template is sent in the email except the merge field values. Following is the code:
trigger EmailOnOpp on Opportunity (after insert) {
for(Opportunity opp:Trigger.new)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toRecipients;
String[] ccRecipients;
String templateApiName = 'New_VF_Template'; // This is the Visualforce template
ID targetObjId;
Id whatId;
ID orgWideEmailId;
Boolean saveAsActivity;
Attachment[] attachList;
Id templateId;
templateId = [select id, name from EmailTemplate where developername = : templateApiName].id;
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
email.setTargetObjectId(opp.Assigned_User__c); // Lookup to the user record.
email.setorgWideEmailAddressId(orgWideEmailId);
email.setTemplateId(templateId);
email.setSaveAsActivity(false); // save email as activity on the targetObjId (i.e. Contact). Note activity can't be saved on Users
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Any help on this is highly appreciated.
Thanks.
Raghu
I am working on a trigger that sends out an email everytime a Opprotunity record is created. I am using Visualforce email template and setting the TemplateId in the trigger code. But when I do this the merge fields are not Populated from the Opportunity record. Entire content of the Visualforce template is sent in the email except the merge field values. Following is the code:
trigger EmailOnOpp on Opportunity (after insert) {
for(Opportunity opp:Trigger.new)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toRecipients;
String[] ccRecipients;
String templateApiName = 'New_VF_Template'; // This is the Visualforce template
ID targetObjId;
Id whatId;
ID orgWideEmailId;
Boolean saveAsActivity;
Attachment[] attachList;
Id templateId;
templateId = [select id, name from EmailTemplate where developername = : templateApiName].id;
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
email.setTargetObjectId(opp.Assigned_User__c); // Lookup to the user record.
email.setorgWideEmailAddressId(orgWideEmailId);
email.setTemplateId(templateId);
email.setSaveAsActivity(false); // save email as activity on the targetObjId (i.e. Contact). Note activity can't be saved on Users
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Any help on this is highly appreciated.
Thanks.
Raghu
It reads, "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."
I'm not sure if this means that you cannot use this method if you are setting the targetObjectId to a User Id, but I'd say it's worth trying to set your Opportunity Id as the WhatId.
I look forward to hearing how it goes!