+ Start a Discussion

How to send email using a template to Queue members?

I tried using the following code as a base and modifying the profile to userID, I am not getting emails, Please advice
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage> ();

List<Profile> userProfile = [select id from profile where Name='System Administrator'];

// getting email template id

List<Id> emailTemplateId = [select id from EmailTemplate where DeveloperName='My_Email_Template'].id; 

for(User sysAdminUser :[Select id from user where ProfileID IN:userProfile]) {
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 string body = 'Hi '+ sysAdminUser.LastName;
 mail.setSubject('Test Subject');
// assign user
//email template id
//this is used to merge field values in email template

 mail.setWhatId( [select id from Account limit 1].id ); 


You didn't have any 'set to address'.....
Try this, and please post any exception you get.

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
List<Profile> userProfile = [select id from profile where Name='System Administrator'];
List<Id> emailTemplateId = [select id from EmailTemplate where DeveloperName='My_Email_Template'];
User[] sysAdminUser = [Select id,LastName from user where ProfileID IN:userProfile];

for(User u : sysAdminUser)
    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    msg.setPlainTextBody('Hi '+ u.LastName)
    msg.setWhatId( [select id from Account limit 1].id );  //not sure what u want here
    msg.setSubject('Test Subject');
    msg.setToAddresses(new string[]{"your emaill address"});

catch(Exception ex)
Hi , Thanks for the response My requirement is to send emails to the members of the queue (They are the partners users with partner licences) Then one lead should be sent to one partner others should not be able to see that Current setup is Leads comes in from other system/bulk upload, Then assignment rule assigns them to a queue (We have to use this as we have internal leads as well) Once assigned they are routed to an appropriate queue with the trigger that we have Appropriate queue is given while uploading it self I was thinking to create a list and pass those as email recipients , I am not getting any emails not sure if something wrong in the code
you can create a list of recipient emails then assign this to the setToAddresses, what exception you got?
I am not getting an exception as its saving and not getting an email
Ankit Kalsara 6Ankit Kalsara 6
Hi all,

Has anyone got the solution to the problem?