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
Staci CaraStaci Cara 

query recipients of an Email Alert/Template

Hi all, 

I want to send an email template to the user that is saved in the recipient field of Email alert. 
I don't know how to query the user in order to put him into:  mail.setToAddresses(;    or  mail.setTargetObjectId(;
Could please someone help me?

So far I have: Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
EmailTemplate template = [SELECT Id, Name, Body, Subject FROM EmailTemplate WHERE DeveloperName = : 'MyEmailTemplate']; EmailMessage message = [SELECT Id, TextBody, Subject, ToAddress FROM EmailMessage WHERE Subject =: template.subject];
mail.setToAddresses(message.toAddress); //at this point it would need a that I don't know how to query
mail.saveAsActivity = false;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

Can please someone help?

Thank you in advance.