You need to sign in to do that
Don't have an account?

Please Help! Reports attachments in email are blank
Hi Everybody,
I have the below Simple apex code
global class ReportTest1 implements System.Schedulable{ global void execute(SchedulableContext sc) { ApexPages.PageReference report = new ApexPages.PageReference('/00O90000003Qn7T?excel=1'); //sandbox Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); attachment.setFileName('TestReport.xls'); attachment.setBody(report.getContent()); attachment.setContentType('text/csv'); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } ); message.setSubject('TEst Report'); message.setHtmlBody('Dear All,<br><br/>Please Find Attached the Report Excel File.<br/>'); message.setToAddresses( new String[] { 'MY EMAIL ID'} ); Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); } }
Now i am scheduling this for running Every 5 hours and the scheduler runs fine , but i get the blank report.
Now if i change the above class to below , I get a perfect Report when CONSTRUCTOR IS invoked but When scheduler is run i get a blank report :(
global class ReportTest1 implements System.Schedulable{ public ReportTest1() { Starter(); } public void Starter() { ApexPages.PageReference report = new ApexPages.PageReference('/00O90000003Qn7T?excel=1'); //sandbox Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); attachment.setFileName('TestReport.xls'); attachment.setBody(report.getContent()); attachment.setContentType('text/csv'); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } ); message.setSubject('TEst Report'); message.setHtmlBody('Dear All,<br><br/>Please Find Attached the Report Excel File.<br/>'); message.setToAddresses( new String[] { 'MY EMAIL ID'} ); Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); } global void execute(SchedulableContext sc) { ApexPages.PageReference report = new ApexPages.PageReference('/00O90000003Qn7T?excel=1'); //sandbox Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); attachment.setFileName('TestReport.xls'); attachment.setBody(report.getContent()); attachment.setContentType('text/csv'); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } ); message.setSubject('TEst Report'); message.setHtmlBody('Dear All,<br><br/>Please Find Attached the Report Excel File.<br/>'); message.setToAddresses( new String[] { 'MY EMAIL ID'} ); Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } ); } }
Kindly I request your expert advise.
Regards,
San
Guys can anybody please help on this have been breaking my head since 2 days :(
Guys is there any solution to this? I also tried to change the permission settings for System to execute all - like export dashboard, etc still it doesnt work :(
Any clues ?
I also think that i must run the scheduler as a user?
Any way to run the scheduler as a user ?
Please help guys
Thanks,
San
Hi,
I am not sure,but you are missing start method before Execute method in batch apex.
Thank you for your help.
I am using this from developer console to schedule
String s = '1 1 06 1-31 1-12 ? ';
ReportTest1 abc = new ReportTest1();
system.schedule('Report Job', s, abc);
Is this wrong?? Please guide.
Regards,
San
You can't get at the content of a page in scheduled apex. You can get at it from the constructor, as you aren't in scheduled context at that time.
Hi Sannn,
I think i found your problem.
your way of scheduling is right.
i think problem lies here,you are using Page Reference in your code,you need to use constructor in your class.
Regards,
Amritesh Singh
Hi Amritesh,
Even if i am using a constructor and invoking constructor by allocating memory to class in scheduler it doesnt work.
What i am saying is this
I wouldn't expect that to work - you are invoking the constructor from an execute method, which means you are in the scheduled context. As the docs clearly state, this is not supported.
HI
I am facing the same problem.
Please help me for the same .
How you resolve yours issue.
Please let me know .....wating for yours reply
Thanks
Check your report filters. If you have added there any field-to-field filters then you need to remove these filters, after removing the field-to-field filters you will get the data in your CSV file.
I was facing the same problem and I have got this message in my CSV file Error: Because this report has field-to-field filters, switch to Lightning Experience to run or edit it. then I have removed the field-to-field filters and after that, I am getting the data.