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 

how did u pickup the owner id of custom object to send mail in apex salesforce?

how did u pickup the owner id of custom object to send mail in apex salesforce?
PriyaPriya (Salesforce Developers) 
Hey Mirza,

In the code you need to querry on the user object and get the email id value for sending email
mirza OSAMAmirza OSAMA
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){
        Test.method2(Trigger.old,Trigger.oldMap);
    }
}
mirza OSAMAmirza OSAMA
hi priya look at above code i tried its not working???????