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
Akis AthanasiadisAkis Athanasiadis 

Export Scheduled Report in excel

I need to have a schedule report exported to excel and put it into a schedule and  send it to specific e-mails.
I have already the report created.

I have used the following code:

global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O58000003m3Sa?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.xls');
        attachment.setBody(report.getContent());
        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[] { 'mplampla.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
        
    }
 }

although I don't see how i give this a schedule.
I guess that this reads the schedule that exists in the report and so the report is sent thourgh this code as an attachment in an email?
Can I also store the executable attachment directly to a path?
Best Answer chosen by Akis Athanasiadis
Akis AthanasiadisAkis Athanasiadis
Any ideas?

All Answers

Akis AthanasiadisAkis Athanasiadis
Any ideas?
This was selected as the best answer
Ravi Dutt SharmaRavi Dutt Sharma
Exporter obj = new Exporter ();

String sch = ‘0 0 1 * * ?’;

String jobIDNew = system.schedule(‘Report run scheduled’, sch, obj);

Run this code in anonymous window in developer console. This will schedule your class to run everyday. Thanks.
Akis AthanasiadisAkis Athanasiadis

Hi Ravi,

I need to run schedule once or twice per week.So if i want to run a schedule every monday on a specific time,how do I set that?

Todd KadasTodd Kadas
@Akis and @Ravi-thanks both for your help.  @Akis-are you receiving attached files in csv format?  I received them as .csv.html, click on the html attachment, and am then able to click again to open in xls as a csv file.  When I forwarded the message to a co-worker, it tried to open in Visual Studio and then crashed his computer.  Any idea how to resolve?
Keith WalkerKeith Walker
You can run a scheduled export of CSV files to your PC or Storage (FTP) on a specific time (day, hour, minute) with the help of Skyvia (https://skyvia.com/data-integration/salesforce-data-loader).