Skip to content

Instantly share code, notes, and snippets.

@peterknolle
Created February 17, 2014 02:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save peterknolle/9043791 to your computer and use it in GitHub Desktop.
Save peterknolle/9043791 to your computer and use it in GitHub Desktop.
Asynchronous Reports with the Analytics API in Apex
<apex:page controller="AsyncReportController" readOnly="true">
<style>
table.reportResults {
width: 100%;
}
</style>
<apex:form >
Select Report:
<apex:selectList value="{!reportId}" multiselect="false" size="1">
<apex:selectOptions value="{!availableReports}"/>
</apex:selectList>
<apex:actionPoller action="{!checkForReportResults}" id="poller" reRender="reportResults" interval="5" enabled="{!reportIsRunning}" />
<apex:commandButton action="{!runReport}" reRender="poller,reportResults" value="Run Report"/>
</apex:form>
<apex:outputPanel id="reportResults" layout="block">
<apex:outputText value="Running..." rendered="{!reportIsRunning}"/>
<apex:outputPanel rendered="{!NOT(reportIsRunning)}">
<table class="reportResults">
<thead>
<apex:repeat value="{!reportResults.reportMetadata.detailColumns}" var="colName">
<th><apex:outputText value="{!reportResults.reportExtendedMetadata.detailColumnInfo[colName].label}"/></th>
</apex:repeat>
</thead>
<tbody>
<apex:repeat value="{!reportResults.factMap['T!T'].rows}" var="row">
<tr>
<apex:repeat value="{!row.dataCells}" var="cell">
<td><apex:outputText value="{!cell.label}"/></td>
</apex:repeat>
</tr>
</apex:repeat>
</tbody>
</table>
</apex:outputPanel>
</apex:outputPanel>
</apex:page>
public with sharing class AsyncReportController {
public List<SelectOption> availableReports { get; set; }
public Id reportId { get; set; }
public Id instanceId { get; set; }
public Boolean reportIsRunning { get; set; }
private transient Reports.ReportResults reportResults;
public AsyncReportController() {
availableReports = retrieveAvailableReports();
}
public List<SelectOption> retrieveAvailableReports() {
List<SelectOption> reptOpts = new List<SelectOption>();
for (Report r : [
Select Id, Name
From Report
Where Format = 'Tabular'
Order By Name
]) {
reptOpts.add(new SelectOption(r.Id, r.Name));
}
return reptOpts;
}
public PageReference runReport() {
Reports.ReportInstance reportInstance = Reports.ReportManager.runAsyncReport(reportId, true);
instanceId = reportInstance.getId();
processInstance(reportInstance);
return null;
}
public PageReference checkForReportResults() {
Reports.ReportInstance reportInstance = Reports.ReportManager.getReportInstance(instanceId);
processInstance(reportInstance);
return null;
}
private void processInstance(Reports.ReportInstance reportInstance) {
reportIsRunning = reportInstance.getStatus() == 'Running' || reportInstance.getStatus() == 'New';
if (!reportIsRunning) {
reportResults = reportInstance.getReportResults();
}
}
public Reports.ReportResults getReportResults() {
return reportResults;
}
}
@peterknolle
Copy link
Author

@chandra2ravi
Copy link

I tried this but unable to get more than 2000 records :(

Please let me know any alternatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment