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
bhanu_prakashbhanu_prakash 

send email to user when oppournity is closed

Hi Team,

send email to user when oppournity is closed, how can we acheive that using trigger
Abdul KhatriAbdul Khatri
How about workflow, no coding
Abdul KhatriAbdul Khatri
Here is the code

Email Class
public class EmailManager {
    // Public method
    public void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
    
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        
        return sendResult;
    }

}

Trigger => You can use the same trigger code for the Lead object but change Opportunity to Lead wherever you can. Also I have hard coded the email address. Change that to your need.
trigger OpportunitySendEmailTrigger on Opportunity (after update) {

    List<Opportunity> opportunityList = new List<Opportunity>();

    for(Opportunity opportunity : trigger.new) {
    
        if(opportunity.StageName != trigger.oldMap.get(opportunity.Id).StageName && opportunity.StageName.contains('Closed')) {
        
            opportunityList.add(opportunity);
            
        }
    }
    
    if(opportunityList .isEmpty()) return;
    
    for(Opportunity oppRecord: opportunityList) {
    
        EmailManager emailMgr = new EmailManager();
        String sbody = 'Please use the link ' 
                    + system.URL.getSalesforceBaseUrl().toExternalForm() 
                    +'/'+oppRecord.Id + ' to go to the record!';
        emailMgr.sendMail('test@test.com', 'Opportunity ' + oppRecord.StageName, sbody);
    }

}