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
SV MSV M 

rigger to send an email to account owner when related opportunity stage changes

I would like to send an email when account-related opportunity stage changes using triggers. The email should contain the updated stage value in the mail template. Can someone help me write the trigger. Thank you...
ShirishaShirisha (Salesforce Developers) 
Hi Sai Vineeth,

Here is the sample code which will send an email whenever the stage value changes on the opportunity record:

public class Email_Opportunity{ public static void email_send(list<Opportunity> opplist) { List<Messaging.SingleEmailMessage > Email_list=new List<Messaging.SingleEmailMessage >(); for(Opportunity op:opplist) { if(op.StageName=='Closed Won') { //op.addError('Email_Opportunity'); System.debug('email send'); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses =new String[] {'xyz@gmail.com','abc@gmail.com'}; message.setSubject('Email test'); String Body_data=''; Body_data=op.Name; message.sethtmlBody(Body_data); Email_list.add(message); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message }); } } } }
==============
You can make changes to the code accoridng to your logic.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri