Please find below code,I am getting attachment in html format. But i need attachment in csv format. Let me know what changes I have to do ?
global class MonthlyScheduler implements System.Schedulable,Database.AllowsCallouts
{
global String ReportId;
global MonthlyScheduler(String ReportId){
this.ReportId = ReportId;
}
global void execute(SchedulableContext sc) {
sendNow(this.ReportId);
}
@future(callout=true)
global static void sendNow(String ReportId){
ApexPages.PageReference report = new ApexPages.PageReference('/'+ReportId+'?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[] { '[email protected]' } );
Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
}
}
ApexPages.PageReference
in anon apex and it worked fine. Try it w/o the @future.