+ Start a Discussion
nununinununi 

Help with Emailing via Apex Class

Hello Developers,
I need to write a schedulable apex class that sends email notifications to users who have not logged in the past 30 days. This is what I have so far but I am getting the error "Error: Compile Error: Method does not exist or incorrect signature: [Messaging.MassEmailMessage].setTargetObjectIds(LIST<User>) at line 12 column 17"

I am not sure where I am going wrong here.
global class NotifyUsers implements Schedulable {

global void execute(SchedulableContext ctx){

    dateTime dt = date.today()-30;    
    id sysAdm = [SELECT id from Profile where Name =: 'System Administrator' LIMIT 1].id;
    List <User> userList = [SELECT Name, Email, LastLoginDate, ISActive, Id From User WHERE IsActive = true AND LastLoginDate <: +dt AND Name != 'IRIS Help' AND ProfileId !=: sysAdm LIMIT 10000];
    
    if(userList.size() > 0)
        {
                Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
                mail.setTargetObjectIds(userList); // List of  ids of the contact, lead, or User
                mail.setTemplateId('00***********XU'); // Id of the email template
                Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
        }
   
}
}

 
Parvinder SinghParvinder Singh
You are passing userlist which is a list of the users to the setTargetObjectIds method which is wrong, you should create a List<Id> and populate it with all the id of users in the userlist.
List<Id> uids= new List<Id>();
for(User u: userlist)
uids.add(u.id);

on your line 12 replace userlist with uilds list.
mail.setTargetObjectIds(uids);
Deepak Kumar ShyoranDeepak Kumar Shyoran
You are passing UserList not Id. This methods need as List of Id of Contact,Lead or User type.