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
Anant Pandey 8Anant Pandey 8 

trigger on case to send email when escalated in checked

I am writing a trigger on case to send an email when excalated field is checked.

Please help me with below code 
 
Trigger caseEscalated on Case (After insert, After Update) {
    
    list<case> caselist = [select id, IsEscalated from case where id in :trigger.new];
      
         
    for(case c:trigger.new){
                      
        if(c.IsEscalated == True){
            
      
        ??? ----- prepare and send an email----- ????  
                      
            
        }
        
    }
    
}

 
Best Answer chosen by Anant Pandey 8
Raj VakatiRaj Vakati
Use like this


https://webkul.com/blog/sending-email-apex/


https://developer.salesforce.com/forums/?id=906F0000000DDszIAG​​​​​​​

http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/​​​​​​​
Trigger caseEscalated on Case (After insert, After Update) {
    
    list<case> caselist = [select id, IsEscalated from case where id in :trigger.new];
      
         List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
    for(case c:trigger.new){
                      
        if(c.IsEscalated == True){
            
     // 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(myContact.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('sirdavid@bankofnigeria.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@bankofnigeria.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + myContact.FirstName + ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);       
            
        }
        
    }
	  Messaging.sendEmail(mails);

    
}

 

All Answers

Raj VakatiRaj Vakati
Use like this


https://webkul.com/blog/sending-email-apex/


https://developer.salesforce.com/forums/?id=906F0000000DDszIAG​​​​​​​

http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/​​​​​​​
Trigger caseEscalated on Case (After insert, After Update) {
    
    list<case> caselist = [select id, IsEscalated from case where id in :trigger.new];
      
         List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
    for(case c:trigger.new){
                      
        if(c.IsEscalated == True){
            
     // 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(myContact.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('sirdavid@bankofnigeria.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@bankofnigeria.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + myContact.FirstName + ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);       
            
        }
        
    }
	  Messaging.sendEmail(mails);

    
}

 
This was selected as the best answer
Anant Pandey 8Anant Pandey 8
Thank you Raj