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
Richa Paliwal 18Richa Paliwal 18 

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}

 
Sharat C 2Sharat C 2
Hi Richa,

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 
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'];             
            //to get merge field intemplate you have to use "setWhatId" and If you want to use setWhatId you must set "setTargetObjectId"
			mail.setTargetObjectId(Con[0].id);
			mail.setWhatId(newItem.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);
	
}

This one should work. Let me know if not

Thanks 
Sharat
Richa Paliwal 18Richa Paliwal 18
HI Sharat, Thank you for your time and feedback. I need to trigger the email to a distibution list (for eg, abc@nwea.org) instead of a specific user, lead or contact. WHat should my targetObjectId be in that case?