For Developers

Run Salesforce Reports in Apex

3 min read
CloudAnswers photo

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.

CloudAnswers photo

About CloudAnswers

Salesforce apps, powerful components, custom development, and consulting. Our experienced team helps you to create and modify workflow processes in salesforce.

Related Articles

For Developers

Designing User Security and Visibility in Salesforce

Trust and security are at the top of Salesforce's priority list. The platform has everything you need if you're looking to construct a robust user security paradigm. However, this security approach has flaws that an attacker can exploit to gain access to your data. The Salesforce Architect has the duty to ensure that these features are set up correctly.

March 16, 2022

7 Min Read

For Developers

Batch Apex Error Event - CloudAnswers Hackathon

A hackathon is an event usually put together by a tech organization. The event brings programmers together over a specific period to collaborate on a project.

June 28, 2021

5 Min Read

For Developers

Save DOM Element As Image Attachment In Salesforce

Use a dom-to-image Javascript library that can turn arbitrary DOM nodes into a vector (SVG) or raster (PNG or JPEG) image in Salesforce.

April 8, 2021

3 Min Read