+ Start a Discussion
VRKVRK 

How to send email notification to email field in custom object through Apex class

Hi folks,
How to send email alert / notificaiton from Case object to '''SLA Custom object ---> Peer reviwer ''' through APex class.
Can you some one pls provide code for this requirement 
Thanks
Sekhar
 
Kalpesh Gohil 11Kalpesh Gohil 11
Hello Sekhar,

Hope you doing well...!

You want to send an email using apex from perticular field value in toAddress then review the below code if this can help you.

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'abc@gmail.com', 'xyz@gmail.com' };
message.optOutPolicy = 'FILTER';
message.subject = 'Opt Out Test Message';
message.plainTextBody = 'This is the message body.';
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success)
{
   System.debug('The email was sent successfully.');
}
else
{
   System.debug('The email failed to send: ' + results[0].errors[0].message);
}


You need to bind your field value to toAddress in this code and you can able to send an email.

Hope this solution can help you..!

Please mark it as best answer if it is usefull for you.

Thanks & Regards
Kalpesh Gohil 
Damira PetreanDamira Petrean
Hi,

I would recommend you to use the EmailManager class provided in Trailhead here:
https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_intro

You can also see there how to use it.
Best regards,
Damira
VRKVRK
Hi 
When i execute this code getting below error , do you have any idea why its getting :
Error Occurred: An Apex error occurred: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]
Email address already exists for 'Approver_email__c' ...But not sure , why this error coming ....any ideas and let me know is anything wrong on my code 
    
    public string toAddress{get;set;}
    public string message{get;set;}

@InvocableMethod  
        public static void sendingEmail(){
        Service_Level_Agreement__c sla = new Service_Level_Agreement__c();  
        Case c = new Case();    
          if(c.Task_Category__c == sla.Task_Category__c) 
          {
           Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();     
            String[] toAddresses =new List<String>{sla.Approver_email__c};
            semail.setToAddresses(toAddresses);
           semail.setSubject('Testing email through apex');
           semail.setBccSender(false);
           semail.setUseSignature(true);
           semail.setPlainTextBody('Dear User, Request for Process Reviewer notification mail ');   
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { semail });   
              
          }
          
           }        
      
   

    }
 
VRKVRK
        'Hi 
 When i execute this code , getting below error :
Error Occurred: An Apex error occurred: System.QueryException: List has more than 1 row for assignment to SObject
But i am sure,    Level_Agreement__c -->    Approver_email__c ( is only one value configured ) ..
Any idea ...how to fix this? and any thing changes done in my code level

public class ReviwerEmailNotification {
 public string toAddress {get;set;}
 public string message {get;set;}

    @InvocableMethod
 public static void sendingEmail(List<Case> lst) {

     set<Id> CaseId = new set<Id>();

    for(Case c : lst){

        CaseId.add(c.Id);
    }
     Case c = [select id,Task_Category__c,CaseNumber from Case where id IN:CaseId]; 
     Level_Agreement__c sla = [select id,Task_Category__c,Process_Approver_email__c from Level_Agreement__c where Task_Category__c =:c.Task_Category__c];

     Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
     String[] toAddresses = new List < String > {sla.Approver_email__c};
    if (!toAddresses.isEmpty()) {
    semail.setToAddresses(toAddresses);
    semail.setSubject('"An apporval is require from your ends for case ID '+ c.CaseNumber);
    semail.setBccSender(false);
    semail.setUseSignature(true);
        semail.setPlainTextBody('  TEST TEST ' );
  );
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
   }

}
}
            
Foram Rana RForam Rana R
Please Try below code:
 
public class ReviwerEmailNotification {
 public string toAddress {get;set;}
 public string message {get;set;}

    @InvocableMethod
 public static void sendingEmail(List<Case> lst) {

     set<Id> CaseId = new set<Id>();

    for(Case c : lst){

        CaseId.add(c.Id);
    }
     List<Case> c = [select id,Task_Category__c,CaseNumber from Case where id IN:CaseId]; 
     List<Level_Agreement__c> sla = [select id,Task_Category__c,Process_Approver_email__c from Level_Agreement__c where Task_Category__c =:c.Task_Category__c];

     Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
     String[] toAddresses = new List < String > {sla[0].Approver_email__c};
    if (!toAddresses.isEmpty()) {
    semail.setToAddresses(toAddresses);
    semail.setSubject('"An apporval is require from your ends for case ID '+ c.CaseNumber);
    semail.setBccSender(false);
    semail.setUseSignature(true);
        semail.setPlainTextBody('  TEST TEST ' );
  );
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
   }

}
}