You need to sign in to do that
Don't have an account?

Emails via Apex Class
Hello,
I am trying to write a schedulable apex class where emails are sent to users who have not logged in past 30 days. Here is what I have but I get the following error:
Error: Compile Error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTargetObjectIds(LIST<User>) at line 14 column 17
global class Class 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 ProfileId !=: sysAdm LIMIT 10000];
if(userList.size() > 0)
{
for(User usr : userList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
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.SingleEmailMessage[] { mail });
}
}
update userList;
}
}
I am trying to write a schedulable apex class where emails are sent to users who have not logged in past 30 days. Here is what I have but I get the following error:
Error: Compile Error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTargetObjectIds(LIST<User>) at line 14 column 17
global class Class 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 ProfileId !=: sysAdm LIMIT 10000];
if(userList.size() > 0)
{
for(User usr : userList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
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.SingleEmailMessage[] { mail });
}
}
update userList;
}
}
example (I haven't checked errors for syntax etc, but this might give you the overview of solution):
All Answers
As the following document states, the method only takes one User ID, and is setTargetObjectId (remove the "s") from your method as welll.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setTargetObjectId
If you do want to send to multiple email addresses, you might loop on the user list, collect email addresses and then set multiple to addresses.
example (I haven't checked errors for syntax etc, but this might give you the overview of solution):