Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Guys i am struggling to cover the below code in a test class.

public void createAttachment(){
        String[] toaddress;
        User usr=[SELECT id,Name,Email FROM User WHERE Name='test user' LIMIT 1];
        if(usr.Email != null){
            toaddress=new String[]{usr.Email};
        EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Reporttemplate' LIMIT 1];
        PageReference pdf = Page.PartnerMonthlyReport;
        // Take the PDF content
        Blob b;
            b = pdf.getContent();  
        catch(Exception ex){
        Document doc= new Document();
        //Get the folder id where the repost is to be saved.
        Folder reportFolder = [select id from Folder where name = 'Monthly Report' limit 1];
        if( != null){
            doc.FolderId = ;
        //set the report name'-'+'Partner Mothly Report';
        //specify the report body
        //insert the report pdf into the document folder
            //save the document in document folder
            insert doc;
        }catch(Exception ex){
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName(Monthandyear+'-'+'Partner Mothly Report.pdf');
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();    
            if( != null){
                msg.setTargetObjectId(; //  Here you can give Id of  User , Lead , Contact 
                msg.setFileAttachments(new Messaging.EmailFileattachment[]{efa});
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });

I have written the test class and have called this method but i am getting an error.Please help or give some guidance how to cover the above method.Thank You.

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body specified in the file attachment: []

share|improve this question

1 Answer 1

up vote 3 down vote accepted

It fails because getContent can't be called in a test method. In fact, it throws a catchable exception that, if uncaught, gives you the precise error:

Methods defined as TestMethod do not support getContent call, test skipped

Since the call fails, and you caught the exception, the blob is null, so the body also ends up null. Instead, you should check if the current context is a test method, and if so, substitute dummy data in its place.


if(Test.isRunningTest()) {
    b = EncodingUtil.base64Decode('AA=='); // zero-length, null-terminated string
} else {
    try {
        b = pdf.getContent();
    } catch(Exception e) {
        // Handle the situation, then skip sending email

You should probably take this time to also learn something new: if you're going to try-catch, don't just debug the exception into oblivion, because you'll be scratching your head when something down the line doesn't work. Instead, check to see if you're handling an error that would happen during real execution, such as a NoAccessException, NullPointerException, etc, and handle it appropriately. Catching "Exception" directly is generally a bad idea unless you report it somehow.

share|improve this answer
Thanks @sfdcfox.I got 100 % code coverage. Ya sure i will try to work on the exception handling. – Shamsher Singh Jun 9 '14 at 5:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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