+ Start a Discussion
Sandeep YadavSandeep Yadav 

Not able to see Lead Data in Email Template on passing recordId to template from Apex Class

Here is what i have done...
Messaging.SendEmailResult[] results;
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setToAddresses(new String[] {ldOwnerMail});
message.setTemplateId([select id from EmailTemplate where DeveloperName='LeadsNewassignmentnotificationSAMPLE'].id);
message.setTargetObjectId(lead.OwnerId);

message.setSaveAsActivity(false);
results = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{message});
How to pass recordId here so Emailtemplate can take it and show further details (Name,Company).


Template is look alike is--
** NEW LEAD STATUS CHANGE NOTIFICATION ***

The following lead's status has been changed.

Lead Name: {!Lead.Name}

Company: {!Lead.Company}

Here is the Lead Detail: {!Lead.Link}

 
Annu ChoudharyAnnu Choudhary
Hi Sandeep,
You need to give Lead record Id for merge field in 'setWhatId' method of SingleEmailMessage Class Like this:-
Messaging.SendEmailResult[] results; Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setToAddresses(new String[] {ldOwnerMail}); message.setTemplateId([select id from EmailTemplate where DeveloperName='LeadsNewassignmentnotificationSAMPLE'].id); message.setTargetObjectId(lead.OwnerId);
message.setSaveAsActivity(false); 
message.setWhatId(LeadRecordId);  //Pass here lead record id then merge field work correct.
results = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{message});
Please select the best answer if it is working for you.
Thanks,
 
Sandeep YadavSandeep Yadav
Hi Annu,

I already did this but, still i'm getting null value in email template. 
Annu ChoudharyAnnu Choudhary
Hi Sandeep,
Can you share your code with me. what did you do?

For reference :
Use it  -- 
message.setWhatId(lead.id);