Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Run Reports
Reports can be run with or without details and can be filtered by setting report metadata. When you run a report, the API returns data for the same number of records that are available when the report is run in the Salesforce user interface.
Run a report synchronously if you expect it to finish running quickly. Otherwise, we recommend that you run reports through the Salesforce API asynchronously for these reasons:
-
Long-running reports have a lower risk of reaching the timeout limit when they are run asynchronously.
-
The Salesforce Reports and Dashboards API via Apex can handle a higher number of asynchronous run requests at a time.
-
Because the results of an asynchronously run report are stored for a 24-hour rolling period, they’re available for recurring access.
Run a Report Synchronously
To run a report synchronously, use one of the ReportManager.runReport() methods. For example:
1// Get the report ID
2List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
3 DeveloperName = 'Closed_Sales_This_Quarter'];
4String reportId = (String)reportList.get(0).get('Id');
5
6// Run the report
7Reports.ReportResults results = Reports.ReportManager.runReport(reportId, true);
8System.debug('Synchronous results: ' + results);Run a Report Asynchronously
To run a report asynchronously, use one of the ReportManager.runAsyncReport() methods. For example:
1// Get the report ID
2List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
3 DeveloperName = 'Closed_Sales_This_Quarter'];
4String reportId = (String)reportList.get(0).get('Id');
5
6// Run the report
7Reports.ReportInstance instance = Reports.ReportManager.runAsyncReport(reportId, true);
8System.debug('Asynchronous instance: ' + instance);