+ Start a Discussion
Brian Oconnell 23Brian Oconnell 23 

How to call an existing email alert from apex class?

I would like to use an existing Email Alert in an Apex class.
I do not want to simply send an email from the class.

The reason is that Administrators will want to change recipients and/or templates, and they should be able to do so using clicks, not code.

Is this possible?
What does the apex class do(The one you want to use for sending email)? is this a trigger handler or visualforce controller? If you can provide this info, i may be able to point you to right direction. 
Amit Chaudhary 8Amit Chaudhary 8

Please check below post
1) http://www.infallibletechie.com/2012/10/how-to-reference-email-template-using.html
2) https://th3silverlining.com/2010/05/08/using-basic-email-templates-within-apex/

You Can use Email Template in Apex Class like below sample code:-
List<EmailTemplate> lstEmailTemplates = [SELECT Id, Body, Subject from EmailTemplate where DeveloperName = 'Your Email Template Unique Name'];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contactId);// Any contact or User id of your record
mail.setToAddresses(new list<string>{'a@teste.com','any@other.com'});

mail.setWhatId(YourCustomObjectRecord.id); // Enter your record Id whose merge field you want to add in template
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

//Please replace "Your Email Template Unique Name" with your template name

Let us know if this will help you
Brian Oconnell 23Brian Oconnell 23
You can use a custom label for the email recipients.  This would allow admins to edit the template and the recipients without changing any code.  Not the same as using a defined email alert, but the admins would still have the same functionality.
Abhishek jain 98Abhishek jain 98
Thanks @Amit Chaudhary 8 for your sample code and explanation.