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
mirza OSAMAmirza 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);
    }
}
K_JlawK_Jlaw
Do you need to use a trigger or can you combine an email alert and record trigger flow? 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Mirza,

Instead coding, you can use process builder/workflow rule to send email alert.

Thanks!!
Gonapati Bhavitha 11Gonapati Bhavitha 11
Hi Mirza,
I am using Record Triggger Flow
getting email
Gonapati Bhavitha 11Gonapati Bhavitha 11
Hi Mirza
This is record Trigger flow
Thanks!!
Bhavitha