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
Force.platformForce.platform 

System.EmailException:

i have trigger to send email to user after record inserted. but there is error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger customerTrigger caused an unexpected exception, contact your administrator: customerTrigger: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.: []: Class.CustomerImpl.sendEmailToCustomer: line 11, column 1

Trigger:
trigger customerTrigger on Customer__c (after insert, after update) {
    List<Customer__C> cust=[select name, Email_Address__c,Comment__C from Customer__C];
     for(Customer__C ct:cust)
     {CustomerImpl c=new CustomerImpl();
      c.sendEmailToCustomer();
     }
}

class:
public class CustomerImpl {
    public void sendEmailToCustomer()
       {
           Customer__C cust=new Customer__C();
           Messaging.SingleEmailMessage msg= new Messaging.SingleEmailMessage();
           msg.setToAddresses(new String[]{Cust.Email_Address__c});
           msg.setSubject('info about session');
           msg.setPlainTextBody(cust.comment__c);
           Messaging.sendEmail(new Messaging.SingleEmailMessage[]{msg});
       }
}
Best Answer chosen by Force.platform
manoj krishnamanoj krishna
 
Customer__C cust=new Customer__C();
Messaging.SingleEmailMessage msg= new Messaging.SingleEmailMessage();
msg.setToAddresses(new String[]{Cust.Email_Address__c});
msg.setSubject('info about session');
msg.setPlainTextBody(cust.comment__c);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{msg});


In the above code, you are creating a new instance of Customer class and referencing it below lines to set the value of setToAddresses,setPlainTextBody but the value of the instance of Customer__C will be null as you have not set the object. Hence you are getting the error.
A crude solution for your problem can be as below
 
public class CustomerImpl{
public static void sendEmailToCustomer(Customer__C cust) { 
Messaging.SingleEmailMessage msg= new Messaging.SingleEmailMessage();
msg.setToAddresses(new String[]{Cust.Email_Address__c});
msg.setSubject('info about session');
msg.setPlainTextBody(cust.comment__c);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{msg});
    }
}


trigger customerTrigger on Customer__c (after insert, after update) {
List<Customer__C> cust=[select name, Email_Address__c,Comment__C from Customer__C];
for(Customer__C ct:cust)
{
CustomerImpl.sendEmailToCustomer(ct);
 }
}