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
krishna1krishna1 

why schedule apex class didn't get content for attachment from visual force page

Hi all,

Schedulable class send attachment email, it taken attachment body empty. but this class run from developer console working fine.what i am wrong here.

schedulable class:

global class ActivesummaryReportsend implements Schedulable
{
  
   global void execute (SchedulableContext ctx)
   {
     sendmail();
   }
 
 
   public void sendmail()
   {
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     PageReference excl=  Page.Report;
     excl.setRedirect(true);
    
     Blob b;
 
       b = excl.getContent();

    
    // Create the email attachment
     Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
     efa.setFileName('Report.xls');
     efa.setContentType('application/vnd.ms-excel');
     efa.setBody(b);
    
     email.setSubject( 'Activity Summary Report');
     email.setToAddresses(new String[] { 'gopikrishna04@gmail.com' });
     email.setPlainTextBody( 'Please Find Attachment  Summary Report.');
     email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    // email.setSaveAsActivity(true);
     Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        
   } 
}
Abdel-Ali BOULIKAAbdel-Ali BOULIKA
Hi,
I think it's impossible... Because as per the documentation, the method PageReference.getContent() can't be used in :
    Triggers
    Scheduled Apex
    Batch jobs
    Test methods
    Apex email services

See:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_PageReference_getContent.htm
krishna1krishna1
ok, thanks for reply Abdei-Ali Boulika,

is there any way overcome this issue ?

Thanks
Abdel-Ali BOULIKAAbdel-Ali BOULIKA
Hi Krishna,
If the report can be implemented using the report builder (with some gaps acceptables by the users), the best option would be to use the standard feature for creating and scheduling reports : http://help.salesforce.com/HTViewHelpDoc?id=reports_schedule.htm&language=en_US

But I guess your report has been implemented as a visualforce page because it couldn't be achieved using the standard report builder...?

So in that case, I can suggest you the following options:
  1. If the users to which the email is sent are able to connect to salesforce (i.e. they are salesforce.com users) => You can just send an email with no attachment, and only a link to download the report (the link could redirect to the visualforce that generates the report, so they will just have to log in to salesforce to download the report).
  2. If the users to which the email is send are NOT able to connect to salesforce (e.g. customers or external contacts...) => You may consider changing the scenario to something like: a salesforce user will manually click on a button to launch the report generation and/or email sending with the report as attachment.

By the way, for the option 2., your code can be re-used and will work fine if you call it from a visualforce page.




krishna1krishna1
Thank you Abdei-Ali Boulika.