function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SannnSannn 

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

 

 

SannnSannn

Guys can anybody please help on this  have been breaking my head since 2 days :(

SannnSannn

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

Amritesh SinghAmritesh Singh

Hi, 

I am not sure,but you are missing start method before Execute method in batch apex.

SannnSannn
Dear Singh,
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
bob_buzzardbob_buzzard

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.

Amritesh SinghAmritesh Singh

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

 

SannnSannn

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

global class ReportTest1 implements System.Schedulable{
public ReportTest1()
{
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) {
        ReportTest1 reportObject = new ReportTest1();
       
     }
}

 

bob_buzzardbob_buzzard

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.

Sumit@TCSSumit@TCS

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

md saif ansarimd saif ansari
Hi Sannn,

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.

User-added image