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 });
        }  
    }
}
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');
|improve this answer|||||
  • 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 Apr 8 '19 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. – Vijay Ganji Apr 9 '19 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 Apr 9 '19 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);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.