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
shashikant pandeyshashikant pandey 

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.: []

Hi Guys,

I am getting this error for test class coverage.
I have used the below method for sendEmail while trying to call method then getting error.
Apex code:

public class sendEmailAttachmentPR {
    public Pricing_Reconfirmation__c prId{get;set;}
    public String primaryContactId {get;set;}
    public String AccountId{get;set;}
    public String sendToemail{get;set;}
    public string subject{get;set;}
    public string fileName{get;set;}
    public string contentType{get;set;}
    public string parentId{get;set;}
    public string attachmentId{get;set;}
    public blob bodyBlob{get;set;}
    public string messageBody{get;set;}
    public string contactId{get;set;}
    
    
    
    public sendEmailAttachmentPR(){
        
        prId = [Select Id,Name,Account__r.Id,Primary_Contact__r.id from Pricing_Reconfirmation__c where id =: ApexPages.currentPage().getParameters().get('id')];
        system.debug('Pricing Reconfirmation Id'+prId);
        primaryContactId = prId.Primary_Contact__r.id;
        AccountId = prId.Account__r.Id;
        system.debug('Primary Contact Id ====>'+primaryContactId);
        system.debug('Account Id ===> '+AccountId);
        
        Contact con = [SELECT ID,Name,Email,Phone from Contact where Id =:primaryContactId];
        sendToemail = con.email;
        contactId = con.Id;
        system.debug('Email Id  =======>'+sendToemail);
        User currentUser = [SELECT Id FROM User Where Id = :UserInfo.getUserId()];
        
        try{
        Attachment  attachFile = [SELECT Id,Name,parentId,LastmodifiedDate,ContentType,body FROM Attachment where parentId=:prId.Id ORDER BY LastmodifiedDate Desc LIMIT 1];
        system.debug('Attachment File Id '+attachFile.id);
        attachmentId = attachFile.Id;
          subject = 'Price Re-Confirmation Doc';
        fileName = attachFile.Name;
        contentType = attachFile.ContentType;
        parentId = attachFile.ParentId;
        bodyBlob = attachFile.body; 
        }
        catch (Exception e){
            ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.Error,'No attachment available for this record. Please reprice and generate the file.' );
            ApexPages.addMessage(msg);
        }
       
    }
    
    
    public pagereference sendEmailAttchament(){
        
        if(sendToemail !='' && sendToemail!= null){
             Messaging.EmailFileAttachment emailAttach = new Messaging.EmailFileAttachment();
            emailAttach.setContentType(contentType);
            system.debug('ContentType ====> '+contentType);
            emailAttach.setFileName(fileName);
            system.debug('FIle Name ===>'+fileName);
            emailAttach.setInline(false);
            emailAttach.Body = bodyBlob;
            system.debug('Body ====> '+bodyBlob);
        
        
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            semail.setEntityAttachments(new Id[]{attachmentId});
            semail.setSubject(subject);
            String[] sendTo = new String[]{sendToemail};
                semail.setToAddresses(sendTo);
            semail.setPlainTextBody(messageBody);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
            system.debug('Mail sent successfully ');
            ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.CONFIRM,'Email Sent Successfully' );
            ApexPages.addMessage(msg);
            
            
        }
        else {
            ApexPages.Message msg1 = new ApexPages.Message(ApexPages.Severity.Error,'No Contact Email Id Available' );
            ApexPages.addMessage(msg1);
        }
            
        return null;
    }

}

Test Class:
@isTest
public class sendEmailAttachmentPRTest {
    
    
    public static string sendToEmail;
    public static String messageBody;
    public static string subject;
    public static string attachmentId;
    public static Blob bodyBlob;
    public static string fileName;
    public static string contentType;
    
    static testMethod void priceReconfirmationTest(){
     
        Account customer = new Account
            (
                Name = 'Test Customer Account ' + '1',
                ParentId = null,
                AccountCategory__c = 'Academia and Research',
                BillingCountryCode = 'SE',
                CustomerName2__c = 'ARBOR MEDICAL CORPORATION LT',
                CustomerName3__c = 'Test Customer Name 3',
                CustomerName4__c = 'Test Customer Name 4',
                SAPCRMAccountID__c = '1002126370 - SAPCRM',
                BEKeyword__c = 'ARBOR MEBE',
                BusinessEntityFirstPart__c = '605022299',
                CustomerType__c = 'Bill-To',
                Inactive__c = false,
                BillingStreet = 'BALTU PR.145',
                BillingPostalCode = '47125',
                BillingCity = 'KAUNAS',
                BillingCountry = 'sweden',
                Local_ID__c = 'Local_ID__c',
                TaxType__c = '432'
            );
        insert customer;
        
      
        
        Contact cont = new Contact();
        cont.FirstName = 'Sahshikant';
        cont.LastName = 'Pandey';
        cont.Email = 'Test@gmail.com';
        cont.Phone = '+91 7878787878';
        cont.AccountId = customer.Id;
        insert cont;
        
      
        Pricing_Reconfirmation__c pr = new Pricing_Reconfirmation__c();
        pr.Name= 'Test Price Reconfirmation';
        pr.Account__c = customer.Id;
        pr.Primary_Contact__c = cont.Id;
        insert pr;
        
        
        bodyBlob= Blob.valueOf('Unit Test Attachment Body');
        Attachment attach = new Attachment();
        attach.Name='Test attachment';
        attach.ContentType ='application/msword';
        attach.CreatedDate=system.today();
        attach.LastModifiedDate = system.today();
        attach.ParentId = pr.Id;
        attach.Body = bodyBlob;
        insert attach;
        
        List<Attachment> attachments = [SELECT Id,Name,body,LastModifiedDate FROM Attachment where parentId=:pr.Id ORDER BY LastmodifiedDate Desc LIMIT 1];
        
        ApexPages.currentPage().getParameters().put('id', pr.Id);
        messageBody = 'Test Body';
        sendToEmail = cont.Email;
        subject = 'Test Subject';
        attachmentId = attach.Id;
           fileName = attach.Name;
        contentType =attach.ContentType;
        sendEmailAttachmentPR semailpr = New sendEmailAttachmentPR();
        semailpr= new sendEmailAttachmentPR();
        semailpr.sendEmailAttchament();
          
    }
}

Could anyone help me out here to cover the test class method?
Thanks in advance!
Shashikant
AnudeepAnudeep (Salesforce Developers) 
Hi Shashikant, 

You have defined your messageBody attribute and used it in the setPlainTextBody method without populating the messageBody anywhere

public string messageBody{get;set;}
semail.setPlainTextBody(messageBody);

You will see this error when the messageBody is null. Populate it in your method and run your code again. See this post as a reference

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep