Sunday, 22 March 2015

Accessing Report data using "Salesforce1 Reporting API via Apex"

This post explains you a basic code structure for fetching data from an existing report and display it in visualforce page.

Report Screenshot





Visualforce Page Screenshot


Visualforce Page

<apex:page controller="OpportunityReportUsingAnalyticsAPI" readOnly="true">
<apex:pageMessages/>
<apex:form> 
  <apex:outputPanel id="reportResults" layout="block" style="overflow: auto;width:100%;height:435px;">
    <apex:outputText value="Fetching results. Please wait!" rendered="{!IsReportRunning}"/>
    <apex:pageBlock rendered="{!NOT(IsReportRunning)}" title="Opportunity Report.....">
    <table style="width: 100%;">
        <thead>
            <apex:variable value="1" var="count"/>
            <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:pageBlock>
  </apex:outputPanel>
<apex:actionPoller id="poller" reRender="reportResults" interval="5" action="{!checkReportRunningStatus}" enabled="{!IsReportRunning}" />
</apex:form>
</apex:page>


Apex Controller

public with sharing class OpportunityReportUsingAnalyticsAPI
{
    // get reportId from URL
    public Id reportId=ApexPages.currentPage().getParameters().get('reportId');
    public Id instanceId { get; set; }
   
    //flag to indicate whether report is still running or not
    public Boolean IsReportRunning { get; set; }
   
    // stores reportresults
    private transient Reports.ReportResults reportResults;         
   
    // constructor
    public OpportunityReportUsingAnalyticsAPI()
    {                  
        if(reportId!=null)
        {            
            Reports.ReportInstance reportInstance = Reports.ReportManager.runAsyncReport(reportId,true);
            instanceId = reportInstance.getId();
            checkReportRunningStatus();
        }
        else
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Invalid URL, reportId is missing'));
        }
    }
   
    // method to get and populate reportresults
    public PageReference checkReportRunningStatus()
    {
        Reports.ReportInstance reportInstance = Reports.ReportManager.getReportInstance(instanceId);     
        IsReportRunning = reportInstance.getStatus() == 'Running' || reportInstance.getStatus() == 'New';
        if (!IsReportRunning)
        {
            reportResults = reportInstance.getReportResults();
        }       
        return null;
    }

    // returns reportresults
    public Reports.ReportResults getReportResults()
    {
        return reportResults;
    }

}

This code structure remains the same for any apex implementing this API like filtering report dynamically based on filters applied in visual force page.

No comments:

Post a Comment