+ Start a Discussion

About export report data to email by apex class

Hi all,
I have a question that need you help..
Now i wanna use the apex to scheduling the report to export and sent it to an email, the code is below:

global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O9000000213Md?excel=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] {'tony@gmail.com'} );//,
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
but the report.getContent() ofthe highlight line is not useful because it can't get data from report.
Do you know what should i do for it? My pleasure for you give me any suggestion...




I wouldn't think that the schedulable class can reference anything VF (Apexpages).


Instead, you need to define a constructor for Exporter where the VF page can pass in all the variables known to the VF controller. These get passed by value, not reference, so the schedulable object, when it executes, is truly asynchronous.  Thus, you can pass in the report body




Thanks for your reply,but i havent use VF to reference. I mean the schedule code cannot work fine, i can't get any data from the report by this code, the method report.getContent() is not work...hope you reply.


zjlg:  Apologies here ...Would you be so kind as to identify how your schedulable class gets scheduled?  There are many ways to do this:


1 - Via a VF page that a user clicks a button - that causes the controller to issue a System.schedule() method


2 - Via a trigger


3 - Via the path: Setup |  Develop | Apex Classes 'Schedule Apex' button


In cases 3, ApexPages.xxx methods have no page context and I'd be astonished if they worked. My earlier remarks thought you were doing option 1