+ Start a Discussion
sf_evolutionsf_evolution 

Scheduling a report to export as CSV and email to a recipient.

 

Hi all,

I'm trying an approach to a problem I'm having of scheduling a report to return a CSV file to a specified user via email.

I'm doing this by scheduling a VF page via a HTTP callout.

 

All indications in the debug logs seem to say all is working, but I never get the results emailed to me.

The email address and the report ID are stored in a Custom settings group down in the Exporter class...

 

My Scheduled class is as follows

global class ReportExporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
    getmailReportOutput gem = new getmailReportOutput();
    gem.runReport();
    }
}

 The class that is doing the HTTP callout to the VF page calling the class that generates and emails the report:

public class getmailReportOutput{
    public Static String strSession;
    public getmailReportOutput() {
        }
    
    public void runReport() {
strSession = UserInfo.getSessionId(); makeReportRequest(); } @future (callout=true) public static void makeReportRequest() { ReportName__c mc = ReportName__c.getValues('ReportName'); String strEmailAddr = mc.RptEmail__c; URL xHost = System.URL.getSalesforceBaseUrl(); String strHost =xHost.getHost(); String requestUrl = 'https://' + strHost + '/apex/TestSendReport'; HttpRequest req = new HttpRequest(); req.setEndpoint(requestUrl); req.setMethod('GET'); req.setHeader('Cookie','sid=' + strSession;); String output = new Http().send(req).getBody(); } }

 

The VF page that calls the class that generates the report and emails the CSV file is very simple - it just calls a controller and a method that builds the attachmnent and sends it::

 

<apex:page controller="Exporter" action="{!executeRpt}">
  <!-- Begin Default Content REMOVE THIS -->  
  <!-- End Default Content REMOVE THIS -->
</apex:page>

 

The page controller is as follows;

public class Exporter {
    public static Boolean isTest;
    public static String strEmailAddr;
    
    public void executeRpt() {
        System.debug('CALLING REPORT EXPORTER...');        
        ReportName__c mc = ReportName__c.getValues('ReportName');
        strEmailAddr =  mc.RptEmail__c;
        
        System.debug('ReportName:' +  mc.RptName__c);
        System.debug('EmailAddr:' + mc.RptEmail__c);
        integer a = 1;
        ApexPages.PageReference report = new ApexPages.PageReference( '/' + mc.RptName__c + '?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[] { strEmailAddr } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );        
    }
 
}

 

When I schedule the ReportExporter class to run, the debug log on the developer console doesn't seem to show any obvious errors:

 

 27.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
 09:38:36.303 (303605000)|EXECUTION_STARTED
 09:38:36.303 (303665000)|CODE_UNIT_STARTED|[EXTERNAL]|066P00000008yb8|VF: /apex/TestSendReport
 09:38:36.305 (305060000)|CODE_UNIT_STARTED|[EXTERNAL]|01pP0000000DlLl|Exporter <init>
 09:38:36.305 (305077000)|SYSTEM_MODE_ENTER|true
 09:38:36.305 (305682000)|VARIABLE_SCOPE_BEGIN|[2]|Exporter.isTest|Boolean|false|true
 09:38:36.305 (305708000)|VARIABLE_SCOPE_BEGIN|[3]|Exporter.strEmailAddr|String|false|true
 09:38:36.306 (306327000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:15
 09:38:36.306 (306345000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:36.306 (306372000)|METHOD_ENTRY|[1]|01pP0000000DlLl|Exporter.Exporter()
 09:38:36.306 (306384000)|STATEMENT_EXECUTE|[1]
 09:38:36.306 (306394000)|STATEMENT_EXECUTE|[1]
 09:38:36.306 (306403000)|METHOD_EXIT|[1]|Exporter
 09:38:36.306 (306423000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
 09:38:36.306 (306441000)|VARIABLE_SCOPE_BEGIN|[1]|this|Exporter|true|false
 09:38:36.306 (306511000)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x16d8f5e4
 09:38:36.306 (306530000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1
 09:38:36.306 (306567000)|VARIABLE_SCOPE_BEGIN|[2]|this|system.ApexBaseClass|true|false
 09:38:36.306 (306609000)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x16d8f5e4
 09:38:36.306 (306631000)|STATEMENT_EXECUTE|[1]
 09:38:36.306 (306648000)|CODE_UNIT_FINISHED|Exporter <init>
 09:38:36.306 (306707000)|CODE_UNIT_STARTED|[EXTERNAL]|01pP0000000DlLl|Exporter invoke(executeRpt)
 09:38:36.306 (306753000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
 09:38:36.306 (306771000)|VARIABLE_SCOPE_BEGIN|[30]|this|Exporter|true|false
 09:38:36.306 (306812000)|VARIABLE_ASSIGNMENT|[30]|this|{}|0x16d8f5e4
 09:38:36.306 (306834000)|STATEMENT_EXECUTE|[30]
 09:38:36.306 (306841000)|STATEMENT_EXECUTE|[31]
 09:38:36.306 (306884000)|HEAP_ALLOCATE|[31]|Bytes:26
 09:38:36.307 (307021000)|SYSTEM_METHOD_ENTRY|[31]|System.debug(ANY)
 09:38:36.307 (307057000)|USER_DEBUG|[31]|DEBUG|CALLING REPORT EXPORTER...
 09:38:36.307 (307067000)|SYSTEM_METHOD_EXIT|[31]|System.debug(ANY)
 09:38:36.307 (307078000)|STATEMENT_EXECUTE|[32]
 09:38:36.307 (307089000)|HEAP_ALLOCATE|[32]|Bytes:10
 09:38:36.307 (307264000)|SYSTEM_METHOD_ENTRY|[32]|ReportName__c.getValues(String)
 09:38:36.308 (308279000)|HEAP_ALLOCATE|[32]|Bytes:48
 09:38:36.308 (308301000)|SYSTEM_METHOD_EXIT|[32]|ReportName__c.getValues(String)
 09:38:36.308 (308315000)|VARIABLE_SCOPE_BEGIN|[32]|mc|ReportName__c|true|false
 09:38:36.308 (308551000)|VARIABLE_ASSIGNMENT|[32]|mc|{"serId":1,"value":{"Name":"ReportName","SetupOwnerId":"00DP00000007CFaMAM","RptName__c":"00OP0000000Jp3N","LastModifiedById":"00500000006w214AAA","SystemModstamp":"2013-01-24T19:50:17.000Z","CreatedById":"00500000006w214AAA","RptEmail__c":"user@city (13 more) ...","CreatedDate":"2013-01-24T01:04:12.000Z","IsDeleted":false,"LastModifiedDate":"2013-01-24T19:50:17.000Z","Id":"a0LP0000005FKc4MAG"}}|0x77cda7ca
 09:38:36.308 (308570000)|STATEMENT_EXECUTE|[33]
 09:38:36.308 (308632000)|VARIABLE_ASSIGNMENT|[33]|Exporter.strEmailAddr|"user@city (13 more) ..."
 09:38:36.308 (308644000)|STATEMENT_EXECUTE|[35]
 09:38:36.308 (308655000)|HEAP_ALLOCATE|[35]|Bytes:11
 09:38:36.308 (308695000)|HEAP_ALLOCATE|[35]|Bytes:26
 09:38:36.308 (308712000)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY)
 09:38:36.308 (308736000)|USER_DEBUG|[35]|DEBUG|ReportName:00OP0000000Jp3N
 09:38:36.308 (308745000)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY)
 09:38:36.308 (308755000)|STATEMENT_EXECUTE|[36]
 09:38:36.308 (308766000)|HEAP_ALLOCATE|[36]|Bytes:10
 09:38:36.308 (308783000)|HEAP_ALLOCATE|[36]|Bytes:43
 09:38:36.308 (308796000)|SYSTEM_METHOD_ENTRY|[36]|System.debug(ANY)
 09:38:36.308 (308817000)|USER_DEBUG|[36]|DEBUG|EmailAddr:user@citymedia.com
 09:38:36.308 (308825000)|SYSTEM_METHOD_EXIT|[36]|System.debug(ANY)
 09:38:36.308 (308835000)|STATEMENT_EXECUTE|[37]
 09:38:36.308 (308854000)|VARIABLE_SCOPE_BEGIN|[37]|a|Integer|false|false
 09:38:36.308 (308861000)|HEAP_ALLOCATE|[37]|Bytes:4
 09:38:36.308 (308885000)|VARIABLE_ASSIGNMENT|[37]|a|1
 09:38:36.308 (308895000)|STATEMENT_EXECUTE|[42]
 09:38:36.308 (308913000)|HEAP_ALLOCATE|[42]|Bytes:1
 09:38:36.308 (308930000)|HEAP_ALLOCATE|[42]|Bytes:16
 09:38:36.308 (308940000)|HEAP_ALLOCATE|[42]|Bytes:6
 09:38:36.308 (308950000)|HEAP_ALLOCATE|[42]|Bytes:22
 09:38:36.309 (309019000)|VARIABLE_SCOPE_BEGIN|[42]|report|System.PageReference|true|false
 09:38:36.309 (309153000)|VARIABLE_ASSIGNMENT|[42]|report|"/00OP0000000Jp3N?csv=1"|0x5ede6433
 09:38:36.309 (309170000)|STATEMENT_EXECUTE|[45]
 09:38:36.310 (310416000)|VARIABLE_SCOPE_BEGIN|[45]|attachment|Messaging.EmailFileAttachment|true|false
 09:38:36.310 (310654000)|VARIABLE_ASSIGNMENT|[45]|attachment|"common.api.soap.wsdl.EmailFileAttachment@5a8480ed"|0x312c7608
 09:38:36.310 (310671000)|STATEMENT_EXECUTE|[46]
 09:38:36.310 (310683000)|HEAP_ALLOCATE|[46]|Bytes:10
 09:38:36.310 (310743000)|STATEMENT_EXECUTE|[48]
 09:38:36.310 (310769000)|SYSTEM_METHOD_ENTRY|[48]|System.PageReference.getContent()
 09:38:40.061 (4061179000)|SYSTEM_METHOD_EXIT|[48]|System.PageReference.getContent()
 09:38:40.061 (4061307000)|STATEMENT_EXECUTE|[53]
 09:38:40.061 (4061332000)|HEAP_ALLOCATE|[53]|Bytes:8
 09:38:40.061 (4061396000)|STATEMENT_EXECUTE|[54]
 09:38:40.062 (4062321000)|VARIABLE_SCOPE_BEGIN|[54]|message|Messaging.SingleEmailMessage|true|false
 09:38:40.062 (4062508000)|VARIABLE_ASSIGNMENT|[54]|message|"common.api.soap.wsdl.SingleEmailMessage@5605f541"|0x2128aa86
 09:38:40.062 (4062525000)|STATEMENT_EXECUTE|[55]
 09:38:40.062 (4062644000)|HEAP_ALLOCATE|[55]|Bytes:4
 09:38:40.062 (4062722000)|SYSTEM_CONSTRUCTOR_ENTRY|[55]|<init>()
 09:38:40.062 (4062778000)|SYSTEM_CONSTRUCTOR_EXIT|[55]|<init>()
 09:38:40.062 (4062818000)|SYSTEM_METHOD_ENTRY|[55]|LIST<Messaging.EmailFileAttachment>.add(Object)
 09:38:40.062 (4062836000)|SYSTEM_METHOD_EXIT|[55]|LIST<Messaging.EmailFileAttachment>.add(Object)
 09:38:40.063 (4063046000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:40.063 (4063089000)|STATEMENT_EXECUTE|[56]
 09:38:40.063 (4063103000)|HEAP_ALLOCATE|[56]|Bytes:6
 09:38:40.063 (4063147000)|STATEMENT_EXECUTE|[57]
 09:38:40.063 (4063160000)|HEAP_ALLOCATE|[57]|Bytes:23
 09:38:40.063 (4063192000)|STATEMENT_EXECUTE|[58]
 09:38:40.063 (4063211000)|HEAP_ALLOCATE|[58]|Bytes:4
 09:38:40.063 (4063228000)|SYSTEM_CONSTRUCTOR_ENTRY|[58]|<init>()
 09:38:40.063 (4063255000)|SYSTEM_CONSTRUCTOR_EXIT|[58]|<init>()
 09:38:40.063 (4063281000)|SYSTEM_METHOD_ENTRY|[58]|LIST<String>.add(Object)
 09:38:40.063 (4063296000)|SYSTEM_METHOD_EXIT|[58]|LIST<String>.add(Object)
 09:38:40.063 (4063345000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:40.063 (4063372000)|STATEMENT_EXECUTE|[59]
 09:38:40.063 (4063413000)|HEAP_ALLOCATE|[59]|Bytes:4
 09:38:40.063 (4063462000)|SYSTEM_CONSTRUCTOR_ENTRY|[59]|<init>()
 09:38:40.063 (4063493000)|SYSTEM_CONSTRUCTOR_EXIT|[59]|<init>()
 09:38:40.063 (4063513000)|SYSTEM_METHOD_ENTRY|[59]|LIST<Messaging.SingleEmailMessage>.add(Object)
 09:38:40.063 (4063528000)|SYSTEM_METHOD_EXIT|[59]|LIST<Messaging.SingleEmailMessage>.add(Object)
 09:38:40.063 (4063570000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:40.063 (4063591000)|SYSTEM_METHOD_ENTRY|[59]|Messaging.sendEmail(LIST<Messaging.Email>)
 09:38:40.063 (4063704000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:40.095 (4095751000)|EMAIL_QUEUE|[59]|subject: Report, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [user@citymedia.com], plainTextBody: The report is attached., 
 09:38:40.095 (4095970000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
 09:38:40.095 (4095995000)|SYSTEM_METHOD_EXIT|[59]|Messaging.sendEmail(LIST<Messaging.Email>)
 09:38:40.223 (4223309000)|CODE_UNIT_FINISHED|Exporter invoke(executeRpt)
 09:38:40.224 (4224422000)|VF_APEX_CALL|j_id0|{!executeRpt}|PageReference: none
 09:38:40.553 (4335699000)|CUMULATIVE_LIMIT_USAGE
 09:38:40.553|LIMIT_USAGE_FOR_NS|(default)|
   Number of SOQL queries: 0 out of 100
   Number of query rows: 0 out of 50000
   Number of SOSL queries: 0 out of 20
   Number of DML statements: 0 out of 150
   Number of DML rows: 0 out of 10000
   Number of code statements: 17 out of 200000
   Maximum heap size: 0 out of 6000000
   Number of callouts: 0 out of 10
   Number of Email Invocations: 1 out of 10
   Number of fields describes: 0 out of 100
   Number of record type describes: 0 out of 100
   Number of child relationships describes: 0 out of 100
   Number of picklist describes: 0 out of 100
   Number of future calls: 0 out of 10
 
 09:38:40.553|CUMULATIVE_LIMIT_USAGE_END
 
 09:38:40.335 (4335749000)|CODE_UNIT_FINISHED|VF: /apex/TestSendReport
09:38:40.335 (4335762000)|EXECUTION_FINISHED
 

 ....But the email never arrives.

 

When I just run the page (https://c.cs4.visual.force.com/apex/TestSendReport?csv=1)  it runs and send an email just fine.


Am I missing something?

 

Many Thanks for any insight

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sf_evolutionsf_evolution

 

Well, after much rethinking, I go this to work.


For anyone interested, I blogged the solution here:

 

    http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send