You need to sign in to do that
Don't have an account?

Trigger to send email template that has opportunity merge field, from opportunity record.
I need to create a trigger that can send email off of opportunity and send the template that has opportunity merge fields.I get the email from this trigger but it is missing the merge field. My trigger looks like below:
trigger ImplementationProject_EmailAlert on Opportunity (after update) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Opportunity newItem : trigger.new) {
if (newItem.StageName == 'Closed Won ')
system.debug('stagename@@' +newItem.stageName);
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//List <Contact> Con= [select id, name from Contact where name =:'Richa Paliwal'];
//system.debug('contact id@@' +Con);
//mail.setTargetObjectId(Con[0].id);
mail.setSenderDisplayName('Salesforce Support');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
EmailTemplate et=[Select id, Subject, Body from EmailTemplate where Name=:'ZSYSGEN_EXPEDITED Close Won Notification'];
system.debug('Template id@@' +et);
mail.setTemplateId(et.id);
mails.add(mail);
}
}
Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);
}
and out of the box template looks like:
The following Opportunity includes Expedited Implementation as a product and has reached Closed Won stage:
Date Closed Won: {!Opportunity.CloseDate}
Opportunity: {!Opportunity.Name}
Opportunity Type: {!Opportunity.Type}
Account: {!Opportunity.Account}
Agency Code: {!Opportunity.Agency_Code__c}
Shipping State: {!Opportunity.Shipping_State__c}
Implementation Note: {!Opportunity.Implementation_Notes__c}
Opportunity Description: {!Opportunity.Description}
Opportunity Link: {!Opportunity.Link}
trigger ImplementationProject_EmailAlert on Opportunity (after update) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Opportunity newItem : trigger.new) {
if (newItem.StageName == 'Closed Won ')
system.debug('stagename@@' +newItem.stageName);
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//List <Contact> Con= [select id, name from Contact where name =:'Richa Paliwal'];
//system.debug('contact id@@' +Con);
//mail.setTargetObjectId(Con[0].id);
mail.setSenderDisplayName('Salesforce Support');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
EmailTemplate et=[Select id, Subject, Body from EmailTemplate where Name=:'ZSYSGEN_EXPEDITED Close Won Notification'];
system.debug('Template id@@' +et);
mail.setTemplateId(et.id);
mails.add(mail);
}
}
Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);
}
and out of the box template looks like:
The following Opportunity includes Expedited Implementation as a product and has reached Closed Won stage:
Date Closed Won: {!Opportunity.CloseDate}
Opportunity: {!Opportunity.Name}
Opportunity Type: {!Opportunity.Type}
Account: {!Opportunity.Account}
Agency Code: {!Opportunity.Agency_Code__c}
Shipping State: {!Opportunity.Shipping_State__c}
Implementation Note: {!Opportunity.Implementation_Notes__c}
Opportunity Description: {!Opportunity.Description}
Opportunity Link: {!Opportunity.Link}
To get value for merge fields in email template you have to initialize "setWhatId" and if you have to use setWhatId then you must Initialize "setTargetObjectId" ( Contact / Lead / User Id). Please find the below code
This one should work. Let me know if not
Thanks
Sharat