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
AnidevAnidev 

Trigger for choosing templates

Hi All,

 

I have the following requirements:

 

  1. a trigger has to be written on the object - "Email Alert", is it possible?
  2. If possible, can i pass a certain "email template" value in the "Email Template" field ?
  3. 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

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

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

Vinit_KumarVinit_Kumar

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.

AnidevAnidev

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

 

 

 

 

Vinit_KumarVinit_Kumar

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

This was selected as the best answer
AnidevAnidev

Thanks Vinit,

 

Worked great.

KishorKishor

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. 

Vinit_KumarVinit_Kumar

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.

KishorKishor

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?

Vinit_KumarVinit_Kumar

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.

 

KishorKishor

Thanks a lot Vinit. I need to check with users whether they want to have custom buttons instead of standard "Send an Email" button.