-2

When I am trying to schedule this below code:

global class exportReport3 implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O90000008euMXAUH?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'hari.a@gmail.com' } );
        message.setCcAddresses(new String[] { 'hari.cc@gmail.com'});
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );

    }
}

Then I got the below Error:

Scheduler: failed to execute scheduled job: jobId: 7079E000024l9gG, class: common.apex.async.AsyncApexJobObject, reason: Callout from scheduled Apex not supported.

Could you please anyone Help me.

1
  • Anyone help me. – Hari Nov 1 '19 at 6:30
0

Implement Database.AllowsCallouts That is , your code will be

global class exportReport3 implements Schedulable, Database.AllowsCallouts { 
    global void execute(SchedulableContext sc) { 
        ApexPages.PageReference report = new ApexPages.PageReference('/00O90000008euMXAUH?csv=1'); 
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
        attachment.setFileName('report.csv'); 
        attachment.setBody(Blob.valueof(report.getContent().toString())); 
        attachment.setContentType('text/csv'); 
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } ); 
        message.setSubject('Report'); 
        message.setPlainTextBody('The report is attached.'); 
        message.setToAddresses( new String[] { 'hari.a@gmail.com' } ); 
        message.setCcAddresses(new String[] { 'hari.cc@gmail.com'}); 
        Messaging.[( new Messaging.SingleEmailMessage[] { message } );

    }
}

If you want to do it in the schedule class you will need to do it in an @future method.

2
  • when I am using @future method ,this one not support to SchedulableContext .like this error: Future methods do not support parameter type of System.SchedulableContext – Hari Nov 5 '19 at 9:40
  • when i am using Database.AllowsCallouts again i got same error:Scheduler: failed to execute scheduled job: jobId: 7073N0000009h7P, class: common.apex.async.AsyncApexJobObject, reason: Callout from scheduled Apex not supported. – Hari Nov 6 '19 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.