0

I have cobbled together this code in an attempt attach the files in the attachment related list to the email on the change of a field in the Expense record (custom object). The code saves but i get a error on updating the expense record

REQUIRED_FIELD_MISSING, Email body is required.: []: Trigger.SendMail: line 21, column 1

I inserted the query in the query editor and it does not appear to return any results, why is it not find any records when there are attachments on the Expenses object.

Here is my code:

 trigger SendMail on Expense__c (after update) {

    for(Expense__c exp : trigger.new){
        if (exp.Status__c == 'Actual'){
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

            // create email content
            email.setToAddresses(new String[]{'x'});
            email.setSubject('Invoice');

            List < Messaging.Emailfileattachment > fileAttachments = new List < Messaging.Emailfileattachment > ();
            for (Attachment a: [select Name, Body, BodyLength from Attachment where ParentId =: exp.Id]) {
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setFileName(a.Name);
                efa.setBody(a.Body);
                fileAttachments.add(efa);
            }

            email.setFileAttachments(fileAttachments);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }  
    }
}

2 Answers 2

1

As the error suggests, you are getting this error because you are not setting the email body.

As per the documentation,

You must specify a value for setTemplateId, setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.

So, you can fix this error by setting the value for any of the above attributes using above methods

Example :

email.setPlainTextBody('Sample Body');
3
  • thank you Vijay for your response. I am now receiving the email but it is not sending the attachments as well from the record. Are you able to help with the missing attachments?
    – Kris Mckee
    Commented Apr 8, 2019 at 23:42
  • Are you sure you have attachments under the expense record on which you are working ? if yes, SOQL query should have returned the results. (Concern : You have your SOQL in for loop). Note : Files also show up under notes and attachment section which are stored in different objects. Commented Apr 9, 2019 at 3:51
  • Based on this, my guess is that they are being stored as files and thus I will need to query this instead, I know this involves Content Document, Content Document Link and Document Version, I will have a crack at it.
    – Kris Mckee
    Commented Apr 9, 2019 at 5:20
0

You need to add body to Messaging.SingleEmailMessage email instance.

Try using these.

 email.setPlainTextBody('Access to the following systems is requested')

or

  String body = 'Testing';
  email.setHtmlBody(body);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .