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
SAHG-SFDCSAHG-SFDC 

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
 
mail.setTargetObjectId(sysAdminUser.Id); 
 mail.setSaveAsActivity(false);
 
//email template id
 
mail.setTemplateId(emailTemplateId); 
 
//this is used to merge field values in email template

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

}
Messaging.sendEmail(mails);

 
doravmondoravmon
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.setTemplateId(emailTemplateId[0].Id);
    msg.setTargetObjectId(u.Id);
    msg.setWhatId( [select id from Account limit 1].id );  //not sure what u want here
    msg.setSaveAsActivity(false);
    msg.setSubject('Test Subject');
    msg.setToAddresses(new string[]{"your emaill address"});

    emails.add(msg);
}
try 
{
    Messaging.sendEmail(emails,false);
}
catch(Exception ex)
{
    system.debug(ex.Message());
}
 
SAHG-SFDCSAHG-SFDC
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
doravmondoravmon
you can create a list of recipient emails then assign this to the setToAddresses, what exception you got?
SAHG-SFDCSAHG-SFDC
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?