Skip to main content
Hi,

 

Please tell how can I send mail to all users in the list

 

Below is my code : 

 

 

global void finish(Database.BatchableContext BC){

    

      AsyncApexJob a = [SELECT Id,Status,JobType,NumberOfErrors,JobItemsProcessed,TotalJobItems,CompletedDate,ExtendedStatus

          FROM AsyncApexJob WHERE Id =:BC.getJobId()];

          

        

         List<User> userList = new List<User>();

         userList = [SELECT Id,Email,IsActive FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = True] ;

          

       // Send an email to all active Admins notifying of job completion.

           

           for(User u : userList)

           {           

               Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

               

               //String[] toAddresses = new String[] {user.Id};

               //mail.setToAddresses(toAddresses);

               mail.setTargetObjectId(u.Id);

               mail.setSubject('Updating Monthly Plan Opportunity Batch - ' + a.Status);

               mail.setSaveAsActivity(false);

               mail.setPlainTextBody

               ('The batch Apex job completed on  ' + a.CompletedDate + ',\n\n' +

               'Job Status : ' + a.Status + '\n'+

               'Total Job Items processed : ' + a.TotalJobItems + '\n'+

               'Number of Job Items processed : ' + a.JobItemsProcessed + '\n' +

               'Number of Failures : '+ a.NumberOfErrors);

               Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

            }        

    }

 

 
2 respostas
  1. 14 de ago. de 2017, 04:50
    Hi 

     

    Please use below code. You just need to change email field api name while inserting to list.

     

    List toAddresses = new List<String>;

     

    for(User u : userList)

     

    {           

     

       toAddress.add(u.emailfieldAPIName);

     

     }  

     

     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

     

       mail.setToAddresses(toAddresses);

     

       //mail.setTargetObjectId(u.Id);

     

       mail.setSubject('Updating Monthly Plan Opportunity Batch - ' + a.Status);

     

       mail.setSaveAsActivity(false);

     

       mail.setPlainTextBody

     

       ('The batch Apex job completed on  ' + a.CompletedDate + ',\n\n' +

     

       'Job Status : ' + a.Status + '\n'+

     

       'Total Job Items processed : ' + a.TotalJobItems + '\n'+

     

       'Number of Job Items processed : ' + a.JobItemsProcessed + '\n' +

     

       'Number of Failures : '+ a.NumberOfErrors);

     

       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

     

    }        

     

    If the above suggestion worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future. 

     

    Thanks & Regards 

     

    David Hales(1044)

     

    Kloudrac Software Pvt. Ltd.

     

     
0/9000