You need to sign in to do that
Don't have an account?
krishna1
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});
}
}
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});
}
}
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
is there any way overcome this issue ?
Thanks
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:
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.