You need to sign in to do that
Don't have an account?
Amar123
email Templatess
I've a custom field which is not being populated in the visual force email which is send. If I test the email template via Setup > My Templates > Send test and verify merge fields, the custom field {!relatedTo.xxx} is populated in the email.
Though when we send the actual email, which is send via a trigger, the field is not populated
Though when we send the actual email, which is send via a trigger, the field is not populated
public class sendEmailHandler {
public static void emailSent(List<Contact> newList){
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Contact con: newList){
if(con.Email != null & con.LastName != null){
EmailTemplate et = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name =: 'Mail Cover Letter'];
//String body=et.HtmlValue;
System.debug('et '+et);
System.debug('et.Body '+et.Body);
System.debug('et.HtmlValue '+et.HtmlValue);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Map<String, String> ncParams=new Map<String, String> {'{lastName}' =>con.LastName,'{firstName}'=> con.FirstName};
String body=setBodyValues(et.HtmlValue,ncParams);
System.debug('body '+body);
List<String> sendTo = new List<String>();
List<String> ccTo = new List<String>();
ccTo.add(UserInfo.getUserEmail());
sendTo.add(con.Email);
mail.setToAddresses(sendTo);
mail.setCcAddresses(ccTo);
mail.setSubject(et.Subject);
mail.setTemplateId(et.id);
//mail.setHtmlBody(et.Body);
mail.setTreatTargetObjectAsRecipient(true);
mail.setTargetObjectId(UserInfo.getUserId());
mail.setSaveAsActivity(false);
mail.setHtmlBody(body);
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
public static void status(List<Contact> oldList){
for(Contact con: oldList){
con.Status__c = 'Endorsed';
}
}
public static String setBodyValues(string body,Map<String,String> params){
if(params!=null && params.size()>0){
for(String key :params.keySet()){
string val=params.get(key);
body=body.replace(key,val);
system.debug('body:'+body);
}
}
return body;
}
}
Trigger:
trigger sendEmail on Contact (before insert) {
if(trigger.isBefore){
if(trigger.isInsert){
sendEmailHandler.emailSent(Trigger.new);
sendEmailHandler.status(Trigger.new);
}
}
}
All Answers
public class sendEmailHandler {
public static void emailSent(List<Contact> newList){
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Contact con: newList){
if(con.Email != null & con.LastName != null){
EmailTemplate et = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name =: 'Mail Cover Letter'];
//String body=et.HtmlValue;
System.debug('et '+et);
System.debug('et.Body '+et.Body);
System.debug('et.HtmlValue '+et.HtmlValue);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Map<String, String> ncParams=new Map<String, String> {'{lastName}' =>con.LastName,'{firstName}'=> con.FirstName};
String body=setBodyValues(et.HtmlValue,ncParams);
System.debug('body '+body);
List<String> sendTo = new List<String>();
List<String> ccTo = new List<String>();
ccTo.add(UserInfo.getUserEmail());
sendTo.add(con.Email);
mail.setToAddresses(sendTo);
mail.setCcAddresses(ccTo);
mail.setSubject(et.Subject);
mail.setTemplateId(et.id);
//mail.setHtmlBody(et.Body);
mail.setTreatTargetObjectAsRecipient(true);
mail.setTargetObjectId(UserInfo.getUserId());
mail.setSaveAsActivity(false);
mail.setHtmlBody(body);
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
public static void status(List<Contact> oldList){
for(Contact con: oldList){
con.Status__c = 'Endorsed';
}
}
public static String setBodyValues(string body,Map<String,String> params){
if(params!=null && params.size()>0){
for(String key :params.keySet()){
string val=params.get(key);
body=body.replace(key,val);
system.debug('body:'+body);
}
}
return body;
}
}
Trigger:
trigger sendEmail on Contact (before insert) {
if(trigger.isBefore){
if(trigger.isInsert){
sendEmailHandler.emailSent(Trigger.new);
sendEmailHandler.status(Trigger.new);
}
}
}
htmlBody = htmlBody.replace('{!Case.CaseNumber}', c.CaseNumber);