0

I am writing a test class with testMethod function. In the function i am trying to send mail from a test class but getting an error. The error is System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]

I think that blob object is empty and that is why i am getting this error, but i tried other ways and really can't solve this problem. The code i am working on is like this. I understand that i should not use getContent in a test but writing if(isRunningTest) {blob b = BLob.valueOf('abcd')} is not helping as well.

Can anyone help me on resolving this??

List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
        for (Id someid: siteQueueMap.keySet()) {
            PageReference pdf = Page.Report;
            Test.setCurrentPage(pdf);
            ApexPages.currentPage().getParameters().put('identifier',someid);
            Blob b = pdf.getContent();
            //Blob b = Blob.valueOf('Unit.test');


            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName(String.format(AttachmentFilenameFormat, new List<String>{ siteNames.get(someid).trim() }));
            efa.setBody(b);

            List<Id> userIds = queueUsersMap.get(siteQueueMap.get(siteId));
            if (userIds != null) {
                for (Id uId : userIds) {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setTemplateId(emailTemplateId);
                    mail.setTargetObjectId(uId);
                    mail.setWhatId(someid);
                    mail.setSaveAsActivity(false);
                    mail.setFileAttachments(new Messaging.EmailFileAttachment[]{ efa });
                    emailList.add(mail);
                }
            }
        }
        Messaging.sendEmail(emailList);
  • Can you add both the code, the main class code which you are trying to cover and the test class code too – Hemant Jain Mar 3 '18 at 7:44
0

getContent is not supported in test method, refer to doc:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_getContent.htm

As an alternative please use the approach provided here:

https://success.salesforce.com/ideaView?id=08730000000HzknAAC

Blob b = blob.valueof('error');
try { b = formPdf.getContent(); }      
catch(Exception e){}

I have tested it using a similar sample code than yours and it works for me:

@isTest
private static void testSendEmailWithAttachement() {
    // Given
    List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
    PageReference pdf = Page.Report;
    Test.setCurrentPage(pdf);

    Blob b = blob.valueof('Unit.test');
    try {b = pdf.getContent(); 
    }catch(Exception e) {}

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('FileName');
    efa.setBody(b);

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTemplateId('00X7F000001Jsnb');
    mail.setTargetObjectId('0057F000000r5pG');
    mail.setSaveAsActivity(false);
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });
    emailList.add(mail);

    // When
    List<Messaging.SendEmailResult> result = Messaging.sendEmail(emailList);

    // Then
    System.assert(result[0].isSuccess(), 'Email not sent');
}

Test execution results:

enter image description here

  • The link you posted has to do with inbound messaging binary attachments, but the original poster is working with outbound email file attachments. – David Cheng Mar 3 '18 at 20:06
  • @DavidCheng I have provided a wrong link, I have updated the answer with the correct one. Regards – JaviCC82 Mar 4 '18 at 22:30
  • @karna can you confirm if the provided approach works for you. – JaviCC82 Mar 5 '18 at 22:55
  • Yes it works for me, the Report page I'm using is just a empty page but the test is working. I have edited the answer to add an screenshot of the executed test. Please if it works for you up vote the answer so that all can take advantage of it. Thanks – JaviCC82 Mar 7 '18 at 20:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.