I try to call the PageReference's getContent() method from a component that is nested in a visualforce email template.
If the template is send via the test button, all looks great. However if I fire the Email alert via workflow rule it got stuck somewhere in the middle of the process.
Console tells email alerts are fired - but no emails are logged (deliverability is set to all emails)
The solution I try to adopt is: http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send
Its quite old but I just can't find any information that getContent() is not supported by email alerts (if I replace the line with the getContent().toString() call with a dummy string all is fired as it should be)
VF Template
<messaging:emailTemplate subject="XLS EXEC" recipientType="User" relatedToType="ReportExport__c">
<messaging:plainTextEmailBody >
TEXT
</messaging:plainTextEmailBody>
<messaging:attachment renderAs="application/x-excel" filename="report.xls" >
<c:ReportOutputter xstrRptId="{!relatedTo.ReportId__c}" xstrEncoding="UTF-8" xstrFormat="xls"/>
</messaging:attachment>
</messaging:emailTemplate>
Component
<apex:component id="ReportOutputter" controller="ReportOutputter_Controller" access="global" allowDML="true">
<apex:attribute name="xstrRptId" description="report ID" type="String" assignTo="{!strRptId}"/>
<apex:attribute name="xstrEncoding" description="file Encoding, can be UTF-8 or ISO 8859-1" type="String" assignTo="{!strEncoding}"/>
<apex:attribute name="xstrFormat" description="file Format, can be xls or csv" type="String" assignTo="{!strFormat}"/>
<apex:outputText value="{!FileStream}" escape="false"/>
Controller
public with sharing class ReportOutputter_Controller {
public String strEmail{get;set;}
public String strRptId{get;set;}
public String strEncoding{get;set;}
public String strFormat{get;set;}
void ReportOutputter_Controller () {}
public String getFileStream() {
return exportReport(strRptId, strEncoding, strFormat);
}
private static String exportReport(String reportName, String encoding, String format) {
String requestURL;
requestURL = '/' + reportName + '?export=1&enc='+encoding+'&xf='+format+'&inline=0';
String pr = new PageReference(requestURL).getContent().toString();
System.debug('### SEND PAGE:'+pr);
return pr;
}
}