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
saud shrumtechsaud 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 });
        }                    
    }
}
 
Best Answer chosen by saud shrumtech
Ajay K DubediAjay K Dubedi
Hi Saud,

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

Raghu NaniRaghu Nani
Hi 
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
 
Ajay K DubediAjay K Dubedi
Hi Saud,

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:
String body = AnniversaryTemplate.HtmlValue;
    body = body.replace('Rohit', Employeelist[index].Name); //Rohit is name used in email template and we are replacing it with fetched name.
    mail.setHtmlBody(body);

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
saud shrumtechsaud shrumtech
how do i get the name of the employee into the mail,that was my question,setTargetObjectIds is not the issue and its commented .HR is getting the mail when employee deletes and updates leave.

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.
 
Raghu NaniRaghu Nani
As Ajay mentioned you have to use below code to replace the {!Leave__c.EmployeeName__c} with actual Employee Name

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)
Ajay K DubediAjay K Dubedi
Hi Saud,

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
This was selected as the best answer
saud shrumtechsaud shrumtech
Thank you .its working