I have an Apex class that contains @future annotation method to send email. A Visualforce page is rendered as pdf and is sent as an email. I'm getting an exception in test class-
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]
Controller code(@future annotated)-
PageReference pdf = Page.ReceiptPrintPage;
pdf.getParameters().put('AccId',parameter1);
pdf.getParameters().put('year',String.valueOf(parameter2));
pdf.setRedirect(true);
// Take the PDF content
Blob b;
try{
b = pdf.getContentAsPDF();
}catch(Exception ex){
System.debug('ex'+ex);
}
// Create the email attachment
String filename = parameter3 + '_receipt_' + parameter2 + '.pdf';
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
//Messaging.EmailFileAttachment.setContentType(param1)
efa.setFileName(filename);
efa.setBody(b);
// Sets the paramaters of the email
String subject = 'Recurring Donations Receipt for the Fiscal Year - ' + parameter2;
body = 'Hello';
email.setSubject(subject);
email.setToAddresses(new String[] {parameter4});
email.setPlainTextBody(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Test code-
PageReference pdf = Page.ReceiptPrintPage;
pdf.getParameters().put('AccId',a1.Id);
pdf.getParameters().put('year',String.valueOf(fiscalYear));
pdf.setRedirect(true);
Blob b;
try{
System.debug('before test content');
//b = pdf.getContentAsPDF();
b = pdf.getContent();
}catch(Exception ex){
System.debug('ex'+ex);
}
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
String filename = 'Receipt_' + fiscalYear + '.pdf';
efa.setFileName(filename);
efa.setBody(b);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String body = 'Testing Email';
String subject = 'Recurring Donations Receipt for the Fiscal Year - ' + fiscalYear;
email.setSubject(subject);
email.setToAddresses(new String[] {'[email protected]'});
email.setPlainTextBody(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
How can I set body for file attachment?