+ Start a Discussion
Obulesh RajObulesh Raj 

how to send email to contact when a new case record is created using trigger?

how to send email to contact when a new case record is created using trigger?
Best Answer chosen by Obulesh Raj
sandeep@Salesforcesandeep@Salesforce
// Send a business proposal to each new Contact
trigger CreateContac on Case  (After insert) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (Case  myCase : Trigger.new) {
    if (myCase.ContactEmail != null && myCase.FirstName != null) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(myCase.ContactEmail);
      mail.setToAddresses(sendTo);
    
      
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'test';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Thanks 
Sandeep 
http://www.codespokes.com/

All Answers

sandeep@Salesforcesandeep@Salesforce
// Send a business proposal to each new Contact
trigger CreateContac on Case  (After insert) {
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (Case  myCase : Trigger.new) {
    if (myCase.ContactEmail != null && myCase.FirstName != null) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(myCase.ContactEmail);
      mail.setToAddresses(sendTo);
    
      
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'test';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Thanks 
Sandeep 
http://www.codespokes.com/
This was selected as the best answer
Obulesh RajObulesh Raj
Thank you