You need to sign in to do that
Don't have an account?
saud shrumtech
Hi, I have written trigger to send a mail to HR when a Employee updates a leave or delete a leave .but am not able to access emp name in mail,how do we get that into mail
Having two objects ,parent object as Employee and field is employee name and another object is Leave where employee name is look up
trigger LeaveUpdatetrigger on Leave__c (after update,after delete) {
if(trigger.isInsert || trigger.isUpdate){
for(Leave__c lv : trigger.new){
EmailTemplate et = [SELECT Id,Subject,HtmlValue, Body FROM EmailTemplate WHERE DeveloperName =:'Update_to_Hr_on_Leave_modification'];
system.debug('--EmailTemplate--'+et);
//String subject = 'TEST';
//String body ='THIS IS ME';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.toAddresses = new String[] { 'xyz@gmail.com' };
mail.setSubject(et.subject);
mail.setHtmlBody(et.HtmlValue);
mail.setplainTextBody(et.body);
// mail.setTargetObjectIds(lv.id);
mail.setTemplateId('00X2v000001H9oK'); //Id of the Email Template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
if(trigger.isdelete){
for(Leave__c lv : trigger.old){
EmailTemplate et = [SELECT Id,Subject,HtmlValue, Body FROM EmailTemplate WHERE DeveloperName =:'Update_to_HR_on_Leave_deletion'];
system.debug('--EmailTemplate--'+et);
//String subject = 'TEST';
//String body ='THIS IS ME';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.toAddresses = new String[] { 'xyz@gmail.com' };
mail.setSubject(et.subject);
mail.setHtmlBody(et.HtmlValue);
mail.setplainTextBody(et.body);
// mail.setTargetObjectIds(lv.id);
mail.setTemplateId('00X2v000001H9oe'); //Id of the Email Template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
trigger LeaveUpdatetrigger on Leave__c (after update,after delete) {
if(trigger.isInsert || trigger.isUpdate){
for(Leave__c lv : trigger.new){
EmailTemplate et = [SELECT Id,Subject,HtmlValue, Body FROM EmailTemplate WHERE DeveloperName =:'Update_to_Hr_on_Leave_modification'];
system.debug('--EmailTemplate--'+et);
//String subject = 'TEST';
//String body ='THIS IS ME';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.toAddresses = new String[] { 'xyz@gmail.com' };
mail.setSubject(et.subject);
mail.setHtmlBody(et.HtmlValue);
mail.setplainTextBody(et.body);
// mail.setTargetObjectIds(lv.id);
mail.setTemplateId('00X2v000001H9oK'); //Id of the Email Template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
if(trigger.isdelete){
for(Leave__c lv : trigger.old){
EmailTemplate et = [SELECT Id,Subject,HtmlValue, Body FROM EmailTemplate WHERE DeveloperName =:'Update_to_HR_on_Leave_deletion'];
system.debug('--EmailTemplate--'+et);
//String subject = 'TEST';
//String body ='THIS IS ME';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.toAddresses = new String[] { 'xyz@gmail.com' };
mail.setSubject(et.subject);
mail.setHtmlBody(et.HtmlValue);
mail.setplainTextBody(et.body);
// mail.setTargetObjectIds(lv.id);
mail.setTemplateId('00X2v000001H9oe'); //Id of the Email Template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
This is your body or html value:
Hi HR MANAGER
This is to inform you that, Mr.{!Leave__c.EmployeeName__c} has modifed his leave application .
You first need to query employee name like: Account a = [SELECT Name FROM Account LIMIT 1];
and then, take templates htmlValue in a string body then, body = body.replace('{!Leave__c.EmployeeName__c}', a.Name);
This will do the job for you.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
All Answers
setTargetObjectIds will accept only User, contact or Lead, to pass merge fields data you have to use setWhatId to pass the object Id
or refer below link to replace the valus in template using apex.
https://salesforce.stackexchange.com/questions/1242/passing-custom-string-into-a-salesforce-email-template
You'll have to fetch body or HtmlValue content in a String variable. Then have to use str.replace() to replace any string in email template. Following code worked fine for me:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This is the mail which HR gets,But he is not able to get name in the mail
Hi HR MANAGER
This is to inform you that, Mr.{!Leave__c.EmployeeName__c} has modifed his leave application .
Thank you & Regards.
body = body.replace('{!Leave__c.EmployeeName__c}', actual Employee Name);
to get the actual Employee Name first you have to query the employee name from employee table or create a formula field in leave object for employee name(look field will give id not name in apex)
This is your body or html value:
Hi HR MANAGER
This is to inform you that, Mr.{!Leave__c.EmployeeName__c} has modifed his leave application .
You first need to query employee name like: Account a = [SELECT Name FROM Account LIMIT 1];
and then, take templates htmlValue in a string body then, body = body.replace('{!Leave__c.EmployeeName__c}', a.Name);
This will do the job for you.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com