Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email with template using trigger

I have the following Trigger:

trigger send_notification on Inquery__c (after update) {

  Inquery__c inquery = trigger.new[0]; 
  String[] toAddresses = new String[] {inquery.email__c}; 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(inquery.OwnerID);
  mail.setSenderDisplayName('Salesforce Support');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);

 if (Trigger.isUpdate) { 
    if(inquery.Quilification__c == 'Qualified') {
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Invitation_to_register_for_Class'];
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r = 
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   
      } 
    if(inquery.Quilification__c == 'Disqualified') {
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Ineligible_course_candidate'];
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r = 
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   
      }   
   }
}

I managed to fix this from its original problem,

And just wanted to share,

Thanks

like image 693
user2333346 Avatar asked Jun 11 '13 15:06

user2333346


People also ask

How do I send a trigger email template in Salesforce?

Go to Setup-> search 'template' -> choose 'Classic Email Templates'-> click on 'New Template' button. Select Visualforce option for 'type of email template'. Enter template name, keep the other defaults. For this example, we use 'Test Account Alert Email'.

Can we send email through trigger?

You can send triggered emails automatically according to pre-defined events, conditions, and customer actions.


1 Answers

I managed to fix it and send the email,

I have updated the code,

i.e. the code above works

like image 137
user2333346 Avatar answered Oct 17 '22 18:10

user2333346