Newer Version Available
レポートデータの取得
ReportResults クラスを使用して、レポートに関連付けられたデータを含むファクトマップを取得できます。
例
ファクトマップのデータ値にアクセスするには、グルーピング値キーを対応するファクトマップキーに対応付けます。次の例では、商談レポートが完了予定月でグループ化されていて、金額項目が集計されていることを前提としています。レポートの最初のグルーピングの集計金額の値を取得する手順は、次のとおりです。
- ReportResults.getGroupingsDown メソッドを使用して、最初の GroupingValue オブジェクトにアクセスし、レポートの最初のダウングルーピングを取得します。
- getKey メソッドを使用して、GroupingValue オブジェクトからグルーピングキー値を取得します。
- '!T' をこのキー値に追加して、ファクトマップキーを作成します。作成されたファクトマップキーは、最初のダウングルーピングの集計値を表します。
- ファクトマップキーを使用して、レポート結果からファクトマップを取得します。
- ReportFact.getAggregates メソッドを使用して、最初の SummaryValue オブジェクトにアクセスし、集計金額値を取得します。
- ReportFactWithDetails.getRows メソッドを使用して、レポートの最初の行の最初のデータセルから項目値を取得します。
1// Get the report ID
2List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
3 DeveloperName = 'Closed_Sales_This_Quarter'];
4String reportId = (String)reportList.get(0).get('Id');
5
6// Run a report synchronously
7Reports.reportResults results = Reports.ReportManager.runReport(reportId, true);
8
9// Get the first down-grouping in the report
10Reports.Dimension dim = results.getGroupingsDown();
11Reports.GroupingValue groupingVal = dim.getGroupings()[0];
12System.debug('Key: ' + groupingVal.getKey());
13System.debug('Label: ' + groupingVal.getLabel());
14System.debug('Value: ' + groupingVal.getValue());
15
16// Construct a fact map key, using the grouping key value
17String factMapKey = groupingVal.getKey() + '!T';
18
19// Get the fact map from the report results
20Reports.ReportFactWithDetails factDetails =
21 (Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
22
23// Get the first summary amount from the fact map
24Reports.SummaryValue sumVal = factDetails.getAggregates()[0];
25System.debug('Summary Value: ' + sumVal.getLabel());
26
27// Get the field value from the first data cell of the first row of the report
28Reports.ReportDetailRow detailRow = factDetails.getRows()[0];
29System.debug(detailRow.getDataCells()[0].getLabel());