You need to sign in to do that
Don't have an account?
mirza OSAMA
Hi everyone..There is a scenario that when opportunity is closed won then user must be notified with email below is my code can someone plz help?
public class test{
public static void method2(list<Opportunity>newlist,Map<id,opportunity>oldmap){
try{
// list<messaging.email>emaillist=new list<Messaging.email>();
map<id,opportunity>oppmap=new map<id,opportunity>();
for(opportunity opp:newlist){
// Opportunity opp=new Opportunity();
// try{
if(oldmap!=null && opp.StageName!=oppmap.get(opp.id).stagename && opp.StageName=='Closed won'){
oppmap.put(opp.ownerid,opp);
// }catch(NullpointerException e){
System.debug('error');
// }
}
}
list<User> userlist=[select id,Email,Name from User where id in:oppmap.keyset()];
list<Messaging.Email>emails=new list<Messaging.email>();
Map<id,String>usermap=new map<id,String>();
for(User u:userlist) {
// if(oppmap.conatinskey(u.id){
usermap.put(u.id,u.email);
System.debug('mirza');
// }
}
Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
for(User u : userList){
list <String> toadd=new LIST<String>();
String e1=usermap.get(u.id);
toadd.add(e1);
msg.setToAddresses(toadd);
msg.setSubject('Test subject');
// msg.setToAddresses(List<String> param1)
msg.setPlainTextBody('test body');
// messaging.email emaillist[]=new Messaging.email();
emails.add(msg);
}
Messaging.sendEmail(emails);
}
catch(exception e){
System.debug(e.getMessage());
}
}
}
------------
trigger trigopp on Opportunity (after update) {
if(Trigger.isUpdate && Trigger.isAfter){
opp8.method2(Trigger.old,Trigger.oldMap);
}
}
public static void method2(list<Opportunity>newlist,Map<id,opportunity>oldmap){
try{
// list<messaging.email>emaillist=new list<Messaging.email>();
map<id,opportunity>oppmap=new map<id,opportunity>();
for(opportunity opp:newlist){
// Opportunity opp=new Opportunity();
// try{
if(oldmap!=null && opp.StageName!=oppmap.get(opp.id).stagename && opp.StageName=='Closed won'){
oppmap.put(opp.ownerid,opp);
// }catch(NullpointerException e){
System.debug('error');
// }
}
}
list<User> userlist=[select id,Email,Name from User where id in:oppmap.keyset()];
list<Messaging.Email>emails=new list<Messaging.email>();
Map<id,String>usermap=new map<id,String>();
for(User u:userlist) {
// if(oppmap.conatinskey(u.id){
usermap.put(u.id,u.email);
System.debug('mirza');
// }
}
Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
for(User u : userList){
list <String> toadd=new LIST<String>();
String e1=usermap.get(u.id);
toadd.add(e1);
msg.setToAddresses(toadd);
msg.setSubject('Test subject');
// msg.setToAddresses(List<String> param1)
msg.setPlainTextBody('test body');
// messaging.email emaillist[]=new Messaging.email();
emails.add(msg);
}
Messaging.sendEmail(emails);
}
catch(exception e){
System.debug(e.getMessage());
}
}
}
------------
trigger trigopp on Opportunity (after update) {
if(Trigger.isUpdate && Trigger.isAfter){
opp8.method2(Trigger.old,Trigger.oldMap);
}
}
Instead coding, you can use process builder/workflow rule to send email alert.
Thanks!!
I am using Record Triggger Flow
Thanks!!
Bhavitha