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
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?

Thanks!
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'){
	     accIds.add(acc.id);
	   }   
   }
   
   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)
     conemails.add();
   }

 
  // 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.setWhatId(OfferId);

	mail.setTemplateId(et.Id);

	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.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
RKSalesforceRKSalesforce
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'){
				accountIds.add(acc.id);
			}			
		}
		for(Contact con : [Select id, Email, Send_Survey__c from Contact where Id IN :accountIds])
        {
			if(con.Send_Survey__c == True){
				sendTo.add(con.Email);
			}			
		}
		EmailTemplate et = [Select Id from EmailTemplate where Name = 'Email Template Name'];	
		mail.setToAddresses(sendTo);
		// Who you are sending the email to
		mail.setTargetObjectId(recipient);

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

Regards,
Ramakant