Newer Version Available

This content describes an older version of this product. View Latest

Filter Reports

To get specific results on the fly, you can filter reports through the API.

Changes to filters that are made through the API don’t affect the source report definition. Using the API, you can filter with up to 20 custom field filters and add filter logic (such as AND and OR). But standard filters (such as range), filtering by row limit, and cross filters are unavailable.

Before you filter a report, it’s helpful to check the following filter values in the metadata.

  • The ReportTypeColumn.getFilterable method tells you whether a field can be filtered.
  • The ReportTypeColumn.filterValues method returns all filter values for a field.
  • The ReportManager.dataTypeFilterOperatorMap method lists the field data types that you can use to filter the report.
  • The ReportMetadata.getReportFilters method lists all filters that exist in the report.

You can filter reports during synchronous or asynchronous report runs.

To filter a report, set filter values in the report metadata and then run the report. The following example retrieves the report metadata, overrides the filter value, and runs the report. The example:
  1. Retrieves the report filter object from the metadata by using the ReportMetadata.getReportFilters method.
  2. Sets the value in the filter to a specific date by using the ReportFilter.setValue method and runs the report.
  3. Overrides the filter value to a different date and runs the report again.
The output for the example shows the differing grand total values, based on the date filter that was applied.
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// Get the report metadata
7Reports.ReportDescribeResult describe = Reports.ReportManager.describeReport(reportId);
8Reports.ReportMetadata reportMd = describe.getReportMetadata();
9
10// Override filter and run report
11Reports.ReportFilter filter = reportMd.getReportFilters()[0];
12filter.setValue('2013-11-01');
13Reports.ReportResults results = Reports.ReportManager.runReport(reportId, reportMd);
14Reports.ReportFactWithSummaries factSum = 
15    (Reports.ReportFactWithSummaries)results.getFactMap().get('T!T');
16System.debug('Value for November: ' + factSum.getAggregates()[0].getLabel());
17
18// Override filter and run report
19filter = reportMd.getReportFilters()[0];
20filter.setValue('2013-10-01');
21results = Reports.ReportManager.runReport(reportId, reportMd);
22factSum = (Reports.ReportFactWithSummaries)results.getFactMap().get('T!T');
23System.debug('Value for October: ' + factSum.getAggregates()[0].getLabel());