You can use the ReportResults class to get the fact map, which contains data that’s associated
with a report.
Example
To access data values of the fact map, you can map grouping
value keys to the corresponding fact map keys. In the following example,
imagine that you have an opportunity report that’s grouped
by close month, and you’ve summarized the amount field. To
get the value for the summary amount for the first grouping in the
report:
- Get the first down-grouping in the report by using the ReportResults.getGroupingsDown method
and accessing the first GroupingValue object.
- Get the grouping key value from the GroupingValue object by using the getKey method.
- Construct a fact map key by appending '!T'to this key value. The resulting fact map key represents
the summary value for the first down-grouping.
- Get the fact map from the report results by using the fact map
key.
- Get the first summary amount value by using the ReportFact.getAggregates method and
accessing the first SummaryValue object.
- Get the field value from the first data cell of the first row
of the report by using the ReportFactWithDetails.getRows method.
List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
DeveloperName = 'Closed_Sales_This_Quarter'];
String reportId = (String)reportList.get(0).get('Id');
Reports.reportResults results = Reports.ReportManager.runReport(reportId, true);
Reports.Dimension dim = results.getGroupingsDown();
Reports.GroupingValue groupingVal = dim.getGroupings()[0];
System.debug('Key: ' + groupingVal.getKey());
System.debug('Label: ' + groupingVal.getLabel());
System.debug('Value: ' + groupingVal.getValue());
String factMapKey = groupingVal.getKey() + '!T';
Reports.ReportFactWithDetails factDetails =
(Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
Reports.SummaryValue sumVal = factDetails.getAggregates()[0];
System.debug('Summary Value: ' + sumVal.getLabel());
Reports.ReportDetailRow detailRow = factDetails.getRows()[0];
System.debug(detailRow.getDataCells()[0].getLabel());