+ Start a Discussion
Kathlene CapobiancoKathlene Capobianco 

Is there an Apex code that can allow me to create a workflow to send out emails to the related contacts of an account when the account status changes to Wrapped?

My company would like to send out a customer satisfaction email (template already created) when a job title (account) wraps. In addition to this the account/contact relationship has a check box for "send survey" and we only want the related contacts that have this check off to receive the email. 
I tried making a workflow to do this but it won't allow me to pull info from the acount related section.

I'm told there is possibly an Apex code that could make this happen. Does anyone have any suggestions?

v varaprasadv varaprasad
Hi Kathlene,

Please check once below sample code : 
trigger statusChanged on account(after update){
   set<id> accIds = new set<id>();
   for(account acc : trigger.new){
       if(acc.status == 'wrap'){
   list<contact> lstContacts = new list<contact>();
   lstContacts = [select id,name,email from contact where accountid in : accIds];
   list<string> conemails = new list<string>();
   for(contact cont : lstContacts){
   if(cont.email != null)

  // create here messaging class use email template and send email:
	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

	EmailTemplate et = [Select Id from EmailTemplate where Name = 'Bypass / Delegated Approval'];



	mail.setToaddresses(new String[] {email});

	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

@For Support: varaprasad4sfdc@gmail.com

Hi Kathlene,

Please use below code . Make changes in the code as required i.e. change field Names as required, Email Template Name:
trigger accountWraps on Account (after insert, after update) {
    if(trigger.isUpdate || trigger.isInsert)
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Employees with higher salary than 7000');
        Set<Id> accountIds = new Set<Id>();
		Set<String> sendTo = new Set<String>();
		for(Account acc : trigger.new)
			if(acc.Status == 'Wrapped'){
		for(Contact con : [Select id, Email, Send_Survey__c from Contact where Id IN :accountIds])
			if(con.Send_Survey__c == True){
		EmailTemplate et = [Select Id from EmailTemplate where Name = 'Email Template Name'];	
		// Who you are sending the email to

		// The email template ID used for the email
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
PLease mark as best answer if helped.