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
Dave The RaveDave The Rave 

Apex Trigger send email with email template on case creation

Hi All,

When a case is created an email should be sent with email template (classic) to the email address from field <case.suppliedemail> and not email address in contact or account object.
  1. Case Created
  2. case.weborigin__c <> BLANK
  3. send email with email template using case.suppliedemail if case.suppliedemail is BLANK use case.contactemail
  4. In the first scenario a case is created via the web-to-case functionality, in the second scenario a user creates a case and links the case to a contact and account. In the first scanario they may not be an account or contact related to the case.
  5. I have several templates based on <case.country__c>
  6. If c.country__c = A then template A should be sent
  7. If c.country__c = B then template B should be sent

trigger Web2case on Case (after insert)
    system.debug('@@@@@@@@@@@@@@@@@' +[0].SuppliedEmail);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();   
    list<String> toAddresses=new list<string>();
    mail.setSenderDisplayName('Salesforce Support');
    mail.setSubject('New Case Created : ' +[0].Id);   
    mail.setPlainTextBody('Your Case: ' +[0].Id +' has been created.');
    mail.setHtmlBody('Your case:<b> ' +[0].Id +' </b>has been created.<p>'+
     'To view your case <a href='[0].Id+'>click here.</a>');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });