I have followed the discussion "Scheduled reports as attachment". The solution given by eyescream looked good but I have a slightly different requirement. I need to run the scheduled report using apex and then save the data in csv, printable and xls formats as salesforce documents. Is it possible to shed some light on this?


thanks for using my thingie :) I'm glad more and more people find it useful (pity it's a volatile solution based on screenscraping)...

Regarding your comment in Greg's post: this generic "script page" means you aren't passing sesssionId (see screen scrape Salesforce with REST GET call from Apex).

But I think you'll have hard time with that in a scheduled class...


For Apex code that is executed asynchronously, such as @future methods, Batch Apex jobs, or scheduled Apex jobs, getSessionId returns null.

The Session ID from scheduled jobs question could help you out. Another way would be to log in from outside (write your scheduled job as for example REST service that would be called from .NET, Java, PHP etc external system... Far from ideal considering the amount of hops we have to jump in order to get it to work. Then again - you could skip the apex part completely and screenscrape from this external tool only?

I've done something nasty in my case - I know for a fact that an integration job runs few minutes before my email so it writes to hidden custom setting the session Id and my scheduled email picks it up. But that's just hack over a hack and indeed Analytics API sounds more promising...

You could also consider paid solutions like Conga. If I recall correctly they're capable of writing their output back as attachment... No idea if anything can be scheduled there though (for same reason I'd suspect their endpoint would have to call login() so you'd have to give them your password, messy)...

|improve this answer|||||
  • eyescream, I have tried "The Session ID from scheduled jobs question could help you out." The solution gave me a Bad Request error. So, I have got the auth token from a client and used it hardcoded it in the request but still I see the same HTML in the document. – Krishna Jun 5 '13 at 20:58
  • 1
    Your report is too big, 3 MB? You should be able to get bigger heap size in a batch/future method (12 MB) or in email service (36 MB). But I think you're doing it wrong, if you need to export/save so much data you should do it from external system or maybe simply use weekly data export... – eyescream Jun 18 '13 at 9:20
  • 1
    Conga, Informatica etc are paid but easy to use. You could use the API (SOAP or REST-based) to pull data out to the system of your choice. developer.force.com has tons of examples for Java, .NET, PHP etc. You could even script Data Loader to be run from commandline (schedule job in your Windows/Linux instead of SF to periodically download a CSV with results of SOQL). Depending how complex your report is, you might have to do some post-processing but generally - SF is a website, it has to be responsive. Data backup(?) like that isn't something I'd call "synchronous operation" so... – eyescream Jun 18 '13 at 19:32
  • 1
    Maybe you'd be best posting it as diff question or editing this one to include your code? What are your callout timeout limits and is total time count close to 2 minutes? – eyescream Jun 25 '13 at 19:58
  • 1
    Sorry. It was my coding error. Corrected it. – Krishna Jun 25 '13 at 20:24

If you want to extend that solution you will need to create a scheduled batch job that runs the ReportExportController class and saves the output string to a salesforce document record. You may need to modify ReportExportController slightly to support being run as a stand-alone class rather than a VF controller.

The other option is to use the new Analytics API which was released as pilot in Summer '13. You'll need to get into the pilot if you need to build this now but this api will be the supported way of accessing reports programmatically going forward.

|improve this answer|||||
  • I tried it Greg and The content of the document looks like this Can you please let me know if you have any other ideas?: <script> if (window.location.replace){ window.location.replace('test.salesforce.com REMOVED CCOMPANY INFO'); } else {; window.location.href ='test.salesforce.com REMOVED CCOMPANY INFO'; } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3.org/TR/html4/loose.dtd"> <html> <head> <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> </head> </html> – Krishna May 22 '13 at 6:40

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.