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
Thanks,

Dave
VICKY_SFDCVICKY_SFDC
trigger Web2case on Case (after insert)
{
    system.debug('@@@@@@@@@@@@@@@@@' + trigger.new[0].SuppliedEmail);
    string emailId=trigger.new[0].SuppliedEmail;
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();   
    list<String> toAddresses=new list<string>();
    toAddresses.add(emailId);
    mail.setToAddresses(toAddresses);
    mail.setReplyTo('support@acme.com');
    mail.setSenderDisplayName('Salesforce Support');
    mail.setSubject('New Case Created : ' + trigger.new[0].Id);   
    mail.setBccSender(false);
    mail.setUseSignature(false);   
    mail.setPlainTextBody('Your Case: ' + trigger.new[0].Id +' has been created.');
 
    mail.setHtmlBody('Your case:<b> ' + trigger.new[0].Id +' </b>has been created.<p>'+
     'To view your case <a href=https://na1.salesforce.com/'+trigger.new[0].Id+'>click here.</a>');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}