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 });
}
}
}