Salesforce提供了強大的報表功能,支持表格、摘要、矩陣以及結合共四種形式,本文探討在站在開發的角度要如何理解報表。
一:查詢報表基本信息
報表在Sales force中是Report對象,基本的查詢語句可以獲取一些報表的基本信息
1 select id,Name,CreatedById,CreatedBy.Username,LastModifiedDate,FolderName from Report
二:在Apex類中運行報表
Salesforce分別提供了runReport(同步)以及runAsyncReport(異步)兩種方式運行報表
1 //同步方法: 2 Reports.ReportResults results = Reports.ReportManager.runReport(reportId,true); 3 //異步方法 4 Reports.ReportInstance instance = Reports.ReportManager.runAsyncReport(m.id, true);
我們寫一段靜態代碼來執行打印報表數據
1 String reportId = '報表id'; 2 Reports.ReportResults results = Reports.ReportManager.runReport(reportId,true); 3 System.debug('===========報表運行結果========>>'); 4 System.debug(results);
需要注意的有兩點
- 沒選中任何列的情況下運行報錯
- 結合報表不支持
沒有選中任何列的報表指的如下圖所示
運行結果:reports.MetadataException: 無法運行報表,因為它尚未選擇任何列。確保通過用戶界面向報表添加字段作為列
至少需要添加一個字段到該報表,之后隱藏詳細記錄,即可實現同樣的效果。
如果報表類型是結合報表,運行上述代碼回提示錯誤
在避開上述兩個易出現的問題后,就能成功用apex方法執行一個報表並得到它的返回值results了。
Results返回值是一個JSON串,ReportResults對象,包含屬性
- allData:標識
- factMap:Map<坐標,數據>存放數據的map
- groupingsAcross:橫向分組字段
- groupingsDown:縱向分組字段
- hasDetailRows:是否有數據
- reportExtendedMetadata:表頭
- reportMetadata:其他信息
實際返回值結構如下
1 Reports.ReportResults[ 2 //public Boolean getAllData() 3 //true, indicates that all report results are returned. 4 //false, indicates that results are returned for the same number of rows as in a report run in Salesforce. 5 allData=true, 6 //public MAP<String,Reports.ReportFact> getFactMap() 7 factMap={},//對象,無序鍵值對 8 //public Reports.Dimension getGroupingsAcross() 9 //Returns a collection of column groupings, keys, and values. 10 groupingsAcross=Reports.Dimension[], 11 //public Reports.Dimension getGroupingsDown() 12 //Returns a collection of row groupings, keys, and values 13 groupingsDown=Reports.Dimension[], 14 //public Boolean getHasDetailRows() 15 //Returns information about whether the fact map has detail rows 16 //true, indicates that the fact map returns values for summary-level and record-level data. 17 //false, indicates that the fact map returns summary values. 18 hasDetailRows=true, 19 //public Reports.ReportExtendedMetadata getReportExtendedMetadata() 20 //Returns additional, detailed metadata about the report, including data type and label information for groupings and summaries. 21 reportExtendedMetadata=Reports.ReportExtendedMetadata[], 22 //public Reports.ReportMetadata getReportMetadata() 23 //Returns metadata about the report, including grouping and summary information 24 reportMetadata=Reports.ReportMetadata[] 25 ]
其中factMap以無序鍵值對的形式存儲了所有報表數據,groupingsAcross和groupingsDown存儲了報表的橫縱字段信息,reportExtendedMetadata記錄了包括報表橫縱分組信息在內的相關信息,reportMetadata則記錄了表格類型,分組長度等信息。
1.查看報表類型
1 Reports.ReportMetadata metadata = results.getReportMetadata(); 2 String reportType = '' + metadata.getReportFormat(); 3 if(reportType == 'TABULAR'){ 4 System.debug('該報表為表格類型'); 5 } 6 if(reportType == 'SUMMARY'){ 7 System.debug('該報表為摘要類型'); 8 } 9 if(reportType == 'MATRIX'){ 10 System.debug('該報表為矩陣類型'); 11 }
2.報表數據獲取
factMap最多支持2*2的矩陣類型,在表格數據的展示上如上表所示,數據顯示格式 *_*!*_*
其中,如果遇到小計/總計的數據,對應的點用T(Total)進行標識,比如最后橫縱交匯的點就是T!T
3.報表分組信息的獲取
1 Reports.ReportExtendedMetadata extended_metadata = results.getReportExtendedMetadata();
1 Reports.ReportExtendedMetadata[ 2 //public MAP<String,Reports.AggregateColumn> getAggregateColumnInfo() 3 //Returns all report summaries such as Record Count, Sum, Average, Max, Min, and custom summary formulas. Contains values for each summary that is listed in the report metadata. 4 aggregateColumnInfo={},//匯總信息 5 //public MAP<String,Reports.DetailColumn> getDetailColumnInfo() 6 //Returns a map of two properties for each field that has detailed data identified by its unique API name. The detailed data fields are also listed in the report metadata. 7 detailColumnInfo={},//詳細信息 8 //public MAP<String,Reports.GroupingColumn> getGroupingColumnInfo() 9 //Returns a map of each row or column grouping to its metadata. Contains values for each grouping that is identified in the groupingsDown and groupingsAcross lists. 10 groupingColumnInfo={}//分組字段 11 ]
橫縱向的兩個分組字段信息可以從groupingColumInfo中獲取,包括了字段的類型,名稱,標簽以及分組級別
4.每個分組下對應顯示的字段
1 Reports.Dimension dim_Across = results.getGroupingsAcross(); 2 List<Reports.GroupingValue> list_gr = dim_Across.getGroupings(); 3 System.debug('橫向' + list_gr); 4 Reports.Dimension dim_Down = results.getGroupingsDown(); 5 List<Reports.GroupingValue> list_gr_down = dim_Down.getGroupings(); 6 System.debug('縱向' + list_gr_down);
5.其他報表信息查詢
1 Reports.ReportMetadata metadata = results.getReportMetadata();
Reports.ReportMetadata提供了關於報表的所有信息,比如報表的id,Name,報表類型,匯總字段,行列分組以及匯總信息依據。我們甚至可以用來修改報表的匯總信息條件。尤為重要的是其提供了groupingsAcross數組,從而能確定報表的分組排列方式,下面是我抽取的一段JSON數據,可以看到報表的分組形式橫向分別是訂單類型(ORDER_TYPE),訂單所有人(ORDER_OWNER),縱向分組是訂單狀態(ORDER_STATUS),訂單等級(Order.DeliveryLevel__c)。
1 groupingsAcross=(Reports.GroupingInfo[ 2 dateGranularity=NONE, 3 name=ORDER_TYPE, 4 sortAggregate=null, 5 sortOrder=ASCENDING 6 ], 7 Reports.GroupingInfo[ 8 dateGranularity=NONE, 9 name=ORDER_OWNER, 10 sortAggregate=null, 11 sortOrder=ASCENDING 12 ]), 13 groupingsDown=(Reports.GroupingInfo[ 14 dateGranularity=NONE, 15 name=ORDER_STATUS, 16 sortAggregate=null, 17 sortOrder=ASCENDING 18 ], 19 Reports.GroupingInfo[ 20 dateGranularity=NONE, 21 name=Order.DeliveryLevel__c, 22 sortAggregate=null, 23 sortOrder=ASCENDING 24 ]),
Salesforce提供了強大的報表功能,但是作為開發者也應該了解其內部的數據存儲,希望本文對你能有所幫助。如有錯漏,歡迎指正,有問題可以留言。