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
mohammadd ismailmohammadd ismail 

Exporting report as csv file

How to get a report into csv format which is outside of salesforce.

Thanks in advance
Best Answer chosen by mohammadd ismail
NagendraNagendra (Salesforce Developers) 
Hi Ismail,

Please find the below code snippet matching your requirement criteria.

Here's an example schedulable class that might be used to email a file. Note that the last five lines need to be ignored or stripped; I'll leave this as an exercise to the reader.
 
You could also build a SOQL query, execute it, then build the CSV yourself, but that means the system is hard-coded and less flexible. Of course, you could build an interactive configuration (perhaps by the use of a custom object or custom settings), which would help work around that particular limitation of flexibility, but that is by far more complex.
global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O500000000000?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        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[] { 'asdf@asdf.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
        
    }
}
Please mark this as the best answer if it helps you.

Best Regards,
Nagendra.P

 

All Answers

Siddharth83JainSiddharth83Jain
Hi,

If you have a pre Built report, execute (RUN) the report. Click on the "Export Details" button. It would provide you an Export File Format option with .xls and .csv. Select the latter and click the EXPORT button. 

Please mark this as Best Answer, if it resolves you query.

Thanks
Siddharth 
OSI Consulting
NagendraNagendra (Salesforce Developers) 
Hi Ismail,

Please find the below code snippet matching your requirement criteria.

Here's an example schedulable class that might be used to email a file. Note that the last five lines need to be ignored or stripped; I'll leave this as an exercise to the reader.
 
You could also build a SOQL query, execute it, then build the CSV yourself, but that means the system is hard-coded and less flexible. Of course, you could build an interactive configuration (perhaps by the use of a custom object or custom settings), which would help work around that particular limitation of flexibility, but that is by far more complex.
global class Exporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O500000000000?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        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[] { 'asdf@asdf.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
        
    }
}
Please mark this as the best answer if it helps you.

Best Regards,
Nagendra.P

 
This was selected as the best answer