我读到有人能够做到这一点,但我很难让它发挥作用。
基本上,我正在为一个页面安排一个 HTTP 标注,该页面有一个控制器,该控制器构建一个 CSV 并将其通过电子邮件发送给收件人。
预定班:
global class ReportExporter implements System.Schedulable {
global void execute(SchedulableContext sc) {
getmailReportOutput ex = new getmailReportOutput();
ex.exportCSV();
}
}
getEmailReportOutput 类:
public class getmailReportOutput{
public Static String strSessionID;
public getmailReportOutput() {
}
public void exportCSV() {
makeReportRequest();
}
@future (callout=true)
public Static void makeReportRequest() {
strHost ='c.cs4.visual.force.com';
strSessionID = UserInfo.getSessionId();
String requestUrl = 'https://' + strHost + '/apex/TestSendReport#';
HttpRequest req = new HttpRequest();
req.setEndpoint(requestUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid=' + strSessionID );
String output = new Http().send(req).getBody();
System.debug('HTTP RESPONSE RETURNED: ' + output);
}
}
getEmailReportOutput 类对 VF 页面执行 HTTP 标注:我确保将 sessionID 与请求一起发送:并且“TestSendReport”只是对控制器的简单标注:
<apex:page controller="Exporter" action="{!runrpt}">
</apex:page>
...并且控制器正在调用报告内容:
public class Exporter {
public static Boolean isTest;
public static String strEmailAddr;
public void runrpt() {
executeRpt();
}
@future
public static void executeRpt() {
System.debug('CALLING REPORT EXPORTER...');
String ReportName__c = '00OP0000000Jp3N';
String strEmailAddr = 'myname@email.com';
ApexPages.PageReference report = new ApexPages.PageReference( '/' + RptName__c + '?csv=1');
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName('report.csv');
attachment.setBody(report.getContent());
attachment.setContentType('text/csv');
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
message.setSubject('Report');
message.setPlainTextBody('The report is attached.');
message.setToAddresses( new String[] { strEmailAddr } );
Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
}
}
...有任何想法吗?调试日志显示一切正常,但没有收到任何内容。我知道这是一堵代码墙,但它似乎是人们建议完成任务的方法——我只是看不出有什么问题。