You need to sign in to do that
Don't have an account?
SAHG-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);
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());
}
Has anyone got the solution to the problem?