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
nununinununi 

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;
}
}
Best Answer chosen by nununi
Hargobind_SinghHargobind_Singh
You can use MassEmailMessage and pass your UserIDs to it. And you won't need the loop as it takes the UserList anyways. Also, I can't figure out why do you need to do an update userList, as you are not doing any update at all... but that is out of scope of this discussion.

example (I haven't checked errors for syntax etc, but this might give you the overview of solution): 
 
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.SingleEmailMessage[] { mail });
           
        }

 

All Answers

Hargobind_SinghHargobind_Singh
Hi,

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. 

 
nununinununi
I want to send an email to all users who get collected in the userList. Can you help me out with the code? I'm kind of lost here. Thanks in advance!
Hargobind_SinghHargobind_Singh
You can use MassEmailMessage and pass your UserIDs to it. And you won't need the loop as it takes the UserList anyways. Also, I can't figure out why do you need to do an update userList, as you are not doing any update at all... but that is out of scope of this discussion.

example (I haven't checked errors for syntax etc, but this might give you the overview of solution): 
 
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.SingleEmailMessage[] { mail });
           
        }

 
This was selected as the best answer