Salesforce Technology
Reports data fetching and traversing in apex

Reports Data Fetching & Traversing in Apex

At times when working with clients and prioritizing reports, there are instances when business dynamics demands for reports data fetching & traversing in apex for a particular data or say set of data. This article intends to educate the readers on how to fetch reports data in apex and project them on a custom vf page.

We want to make functionality which fetches reports data in apex and traverses data and makes a list out of them and shows on an vf page.

4 easy steps for reports data fetching & traversing in apex

In this tutorial, we will guide you how to fetch metadata reports with the help of Metadata api. Keep scrolling to discover the steps involved-

Step 1 : Create Metadata service class in org.

To create a Metadata service class in org., you will require the supporting code. You can either create your own code or click here for a reference.

Step 2 : Use metadata service class for fetching Reports metadata.

Copy the code to use the metadata service class to fetch reports metadata.

/* code block for checking base object of report’s reportType in apex */
MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId();MetadataService.IReadResult res = service.readMetadata(‘Report’, **reportNames__Here**);
for (MetadataService.Metadata md: res.getRecords()) {MetadataService.ReportType mdReport = (MetadataService.ReportType) md;System.debug(‘@@@ reportType: ‘ + mdReport.reportType);}
MetadataService.IReadResult reportType = service.readMetadata(‘ReportType’, **reportTypeNames__Here**);
for (MetadataService.Metadata md: reportType .getRecords()) {MetadataService.ReportType mdReport = (MetadataService.ReportType) md;System.debug(‘@@@ reportType base object: ‘ + mdReport.baseObject);}

Step 3 :- Getting Reports data in apex. 

Here we will fetch the entire report in apex and make a list of Id’s to query this data using SOQL.

List<Report> reportList = [SELECT Id,DeveloperName FROM Report WHERE DeveloperName = :reportSelected ];Set<Id> allSelectedIds = new Set<Id>();Reports.reportResults results = Reports.ReportManager.runReport(reportList[0].Id, true);                    Reports.Dimension dim = results.getGroupingsDown();                    Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get(‘T!T’);                    List<Reports.ReportDetailRow> listOfDetailRow = factDetails.getRows();                    for(Reports.ReportDetailRow detailRow : listOfDetailRow){                        System.debug(‘detailRow—>>’+detailRow);                        System.debug(‘getDataCells—->>’+detailRow.getDataCells());                        String recordId;                        for(Reports.ReportDataCell dataCell : detailRow.getDataCells()){                            if(recordId == null){                                recordId = (String)dataCell.getValue();                            }                                                    }                        if(recordId != null){                            allSelectedIds.add(recordId);                        }                                                                    }                }

Step 4 :- Create a vf page showing a list of data fetched from the report.

Using the code stated below will help you to execute the final step and create the vf page.

<apex:page><apex:pageBlockTable> <apex:column style=”vertical-align: top;”>              <apex:facet name=”header”>Name</apex:facet>       <apex:outputPanel >                        <a href=”{!’/one/’+ l.recordid + ‘/view’}” target=”_blank”>{!l.recordname }</a>              </apex:outputPanel> </apex:column></apex:pageBlockTable></apex:page>

These were the basic steps for reports data fetching & traversing in apex in a systematic order, you will be able to fetch the report with the data arranged in a chronological order. Visit our other blogs to learn more about services or contact us for innovating/managing your personalized business solutions.



Leave a comment

Your email address will not be published. Required fields are marked *