4

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[]{'test@test.com'});

    if(email != null)
      {

        Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});

      }
  }
}
1

There could be any of below reason .

  1. It seems to me that you are not using the correct method to attach a file as email attachment. There is no body method for EmailFileAttachment object

attach.Body = fileBody; --> should really be --> attach.setBody = fileBody;

  1. Avoid hardcoding contentType. Directly use attach.contentType

  2. When you say you get value in body. Ensure that you are checking the attachment body and not the email body.

  3. Also you used Candidate__c and Random__c for looping through trigger.new. Hope both are one and the same.

I tried out the exact code that you used and it works great for me without any error.

  • I have tried attach.setBody = fileBody; syntax before but was still getting the same error. – Animesh Jan 13 '15 at 4:47
  • Hey Amit, I have previously used hardcoded contentType. And the body I am checking is of attachment object for sure. And Candidate and Random are same. – Animesh Jan 13 '15 at 13:33
0

Animesh,

I do not see any DML operations in the code snippet shown above. Can you please post the code snippet for this which results in the trigger being invoked.

The "REQUIRED_FIELD_MISSING" error may occur due to a field or attribute value for a certain mandatory field on the record not being set with a value while performing a DML which in your case would be on the Notes and Attachment standard object.

Also please ensure that the ParentId field on Attachment has a valid record id specified. Refer https://developer.salesforce.com/forums/ForumsMain?id=906F000000096viIAA.

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.