+ Start a Discussion
pranav shah 40pranav shah 40 

How to send an email when opportunity stage is closed won

Hi All,

Can anyone, please help on the same..
not a code required, can u please share the logic.. so that i can work on same as i am new and learning triggers.

Thanks & Regards
Pranav Shah
Best Answer chosen by pranav shah 40
Ajay K DubediAjay K Dubedi

Hi Pranav shah 

Try this and with your email address
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 });
				}
		}
		
		
	}
}
 
trigger Opportunity_email on Opportunity (before update) {
    
    Email_Opportunity.email_send(Trigger.new);

}

 

All Answers

brahmaji tammanabrahmaji tammana
There are multiple ways you can achieve this requirement if no further condition. Few listed below:
  • Workflow > Check the condition on Stagename & Select Created and Everytime its edited. Give Email Alert as action
  • Apex trigger > Write a trigger on Opportunity and send an email by checking old stagename <> Closed WOn and new stagename == Closed Won
Thanks
Brahma
Ajay K DubediAjay K Dubedi

Hi Pranav shah 

Try this and with your email address
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 });
				}
		}
		
		
	}
}
 
trigger Opportunity_email on Opportunity (before update) {
    
    Email_Opportunity.email_send(Trigger.new);

}

 
This was selected as the best answer
pranav shah 40pranav shah 40
Hi @ajay

Why we are using this at Line 5

        List<Messaging.SingleEmailMessage > Email_list=new List<Messaging.SingleEmailMessage >();


                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message }); (At Line 19)


Will you please explain??



Regards
Pranav Shah
Ajay K DubediAjay K Dubedi
It is of no use by mistake I write that comment that line [Line 5]

So if the above code works for you then mark it as the best answer
pranav shah 40pranav shah 40
Hi Ajay,

i am getting Error at Line No 5 :
Error: Compile Error: Variable does not exist: opps at line 5 column 20

public class Email_Opportunity
{
public static void email_send(List<Opportunity> opps);
{
for(Opportunity op:opps)
{
if(op.StageName=='Closed Won')
{
System.debug('Email Send Succsessfully');
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'pranav17shah@gmail.com' };
message.subject = 'Opp Stage Updated';
message.plainTextBody = 'Opportunity Stage Changed to Closed Won';
Messaging.SingleEmailMessage[] messages =new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
}
}
}
}
Ajay K DubediAjay K Dubedi
@ pranav shah 

 I don't know.
 But this code works for me.
 And Instead of line 15 and 16 
 try this
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
And if still it doesn't work then copy my code and execute it.