-
-
Save jdgirald/3668e8a1b01bf2ed2e93b71f85e0b227 to your computer and use it in GitHub Desktop.
Asynchronous Reports with the Analytics API in Apex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment