You need to sign in to do that
Don't have an account?

Trigger for choosing templates
Hi All,
I have the following requirements:
- a trigger has to be written on the object - "Email Alert", is it possible?
- If possible, can i pass a certain "email template" value in the "Email Template" field ?
- If Yes, can i make it dynamic,i.e create/enable and delete/disable email alerts based on certain criterion ?
Many thanks in advance.
Regards
Ani
Hi Ani,
Try using below code in your trigger to send email:-
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id); // The ID of the contact, lead, or user to which the email will be sent.
mail.setTemplateId('00X90000000QHUD'); // Id of the template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
for more methods as how toi use template,please go through below link :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
All Answers
1.) There is no salesforce object as 'Email Alert' ,the object is called 'Email Message' and you can write trigger on it.
2.) There is no field on Email message where you can pass template id.
Hi Vinit,
Thanks for the reply.
I have a simple requirement:
Need to send mail on the click of a button.
For this i am using a combination of trigger and workflow rules(through which email alerts run).
As in the case of standard "send email" feature - i have an option to choose from "email templates", similar thing is what i want to achieve herein.
Hope this clarifies my position.
Regards
Ani
Hi Ani,
Try using below code in your trigger to send email:-
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id); // The ID of the contact, lead, or user to which the email will be sent.
mail.setTemplateId('00X90000000QHUD'); // Id of the template
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
for more methods as how toi use template,please go through below link :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm
Thanks Vinit,
Worked great.
Thanks for the great post.
I have a question in the same lines, on standard email functionaligy. When a template is selected and one of the merge fields is blank or has no data, an error should be displayed when "Send" button clicked and the email should not be sent. Is it possible to write some validation/trigger on standard "Send" button?
Thanks a lot in advance.
Kishor,
You can't play with standard 'Send' button.I would suggest to creat a custom button and then you can use custom coding to play with it i.e. write some JS script to check your criteria.
Thanks a lot for your response Vinit. I appreciate your help.
Have a follow up question: How can I add a custom button to the email page? Don't see any option to edit the page layout. Also, is it possible to access template id that is being selected and merge fields that are being used in the template?
Kishor,
You should ing be adding custom button in the related list of the record where you see 'Send an Email' button,create a new one and add it to the related list of it.
Thanks a lot Vinit. I need to check with users whether they want to have custom buttons instead of standard "Send an Email" button.