In this article, we are going to look into the following key areas

  • How to describe Salesforce reports in Apex
  • How to change report filter values through Apex
  • How to run reports to get data in Apex

Let’s look into each of these topics one by one.

Describe Salesforce Reports in Apex

ReportManager class is available in the Reports namespace in Apex. This class has a method named describeReport which is used to retrieve the report, report type and extended metadata for a tabular, summary, or matrix report. This method takes the Salesforce Id of the report as an input parameter and returns an instance of ReportDescribeResult.

// grab a valid report id from your Salesforce org
String reportId = '00O000000000000000';
Reports.ReportDescribeResult rptDescribe = Reports.ReportManager.describeReport(reportId);

Once you have an instance of ReportDescribeResult, you can use the methods of this class to get report metadata. The following code snippet calls a method to retrieve an instance of ReportMetadata.

// get report metadata
Reports.ReportMetadata rptMetadata = rptDescribe.getReportMetadata();

You can use the report metadata to do several things:

  • Find out what fields and values you can filter on in the report type.
  • Build custom chart visualizations/components by using the metadata information on fields, groupings, detailed data, and summaries.
  • Change filters in the report metadata when you run a report.

Get Report Filters And Change Filter Values in Apex

We can use the “get” methods in the ReportMetadata class to access metadata values. We can use the getReportFilters method to get a list of each custom filter in the report along with the field name, filter operator, and filter value.

// get report filters
List<Reports.ReportFilter> rptFilters = rptMetadata.getReportFilters();

Now, we can iterate through the list of filters and change the value for any filter. The code snippet given below does that. For example, my report has a filter with column name = TYPE and I’m going to change the filter value to Prospect.

// change the report filter value
for(Reports.ReportFilter rptFilter : rptFilters) {
    if(rptFilter.getColumn() == 'TYPE') {
        rptFilter.setValue('Prospect');
    }
}

Please note that getReportFilters method returns only the CUSTOM filters added to the report. If you want to access the standard filters on the report, you can use getStandardFilters method or for accessing standard date filter, you can use getStandardDateFilter method.

One key thing to note here is that there is no way to access the Role Hierarchy standard filter. Let’s try to understand this with help of an example. Create a report by selecting Opportunities with Quotes and Quote Line Items. Open the Filters panel in Lightning Report Builder, and select some role for Show Me filter as presented below.

 

Now, when we describe the report, iterate over custom filters, standard filters and standard date filters, we don’t see the role hierarchy filter in the results.

Get Report Data in Apex

We have looked into how to describe Salesforce reports in Apex and then change get filters and change their values. Now let’s look into how to run Salesforce reports in Apex to retrieve the report data.

// get report data
Reports.ReportResults results = Reports.ReportManager.runReport(reportId, rptMetadata);

Once we have report data, we can utilize it to build chart visualizations, rendering in custom lightning components, and many more as per the use cases.

Are you trying to export and send Salesforce Report data to Non-Salesforce users? Check out the Report Sender tool and download it on Appexchange which is built to take care of that and many other things.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: