0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Salesforce レポート

Last updated at Posted at 2019-06-02

引用:https://www.cnblogs.com/luqinghua/p/9056529.html
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);
需要注意的有两点

没选中任何列的情况下运行报错
结合报表不支持
没有选中任何列的报表指的如下图所示
image.png

运行结果:reports.MetadataException: 无法运行报表,因为它尚未选择任何列。确保通过用户界面向报表添加字段作为列

至少需要添加一个字段到该报表,之后隐藏详细记录,即可实现同样的效果。

image.png

如果报表类型是结合报表,运行上述代码会提示错误

image.png

在避开上述两个易出现的问题后,就能成功用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 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.报表数据获取
image.png

factMap最多支持22的矩阵类型,在表格数据的展示上如上表所示,数据显示格式 _!_*

其中,如果遇到小计/总计的数据,对应的点用T(Total)进行标识,比如最后横纵交汇的点就是T!T

3.报表分组信息的获取

1 Reports.ReportExtendedMetadata extended_metadata = results.getReportExtendedMetadata();
复制代码
1 Reports.ReportExtendedMetadata[
2 //public MAP 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 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 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 list_gr = dim_Across.getGroupings();
3 System.debug('横向' + list_gr);
4 Reports.Dimension dim_Down = results.getGroupingsDown();
5 List 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提供了强大的报表功能,但是作为开发者也应该了解其内部的数据存储,希望本文对你能有所帮助。如有错漏,欢迎指正,有问题可以留言。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?