I have a requirement whenever I create a record and upload a word file document in notes and attachment a trigger gets fired which send that attachment to a desired email address.
I have written this trigger which sends email when a record is created but when I try to add attachment code it shows this error. REQUIRED_FIELD_MISSING, No body specified in the file attachment: But when I check in debug I get the Body as Blob[0].
trigger SendMail on Random__c (after update)
{
Set<ID> RandomIDs = new Set<ID>();
for(Random__c c : trigger.new)
{
RandomIDs.add(c.id);
}
List<Attachment> lstAttach = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId IN : RandomIDs];
for(Candidate__c c : trigger.new)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
for(Attachment att : lstAttach)
{
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(att.Name);
efa.Body = att.body;
efa.setContentType('application/msWord');
efa.setInline(false);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
}
String subject = 'Sample subject ';
email.setSubject(subject);
String body = ' Sample Body ';
email.setPlainTextBody(body);
email.setToAddresses(new String[]{'[email protected]'});
if(email != null)
{
Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
}
}
}