The Reports and Dashboards REST API gives you programmatic access to your report and dashboard data as defined in the report builder and dashboard builder. The API lets you integrate the data into any web or mobile application, inside or outside the Salesforce platform.The Reports And Dashboards Rest API lets you:

  • Run Reports Synchronously or Asynchronously.
  • Get Report Metadata.
  • List Asynchronous Runs of a Report.
  • Filter Reports on Demand.
  • List Recently Viewed Reports.
  • Decode the Fact Map.
  • Save Changes to Reports.
  • Clone Reports.
  • Delete Reports.

But one of the best use of this API can be to use reports data in apex. A sample app to display this ability is described below. For this exercise, we’ll fetch data from a report and display as a chart in a lightning component. The data visualization using chartjs is already covered in a previous post.

So let’s get down to business.

Step 1

Create a summary report. I have created a report using the standard Opportunities report type and grouped by LeadSource.

Step 2

Run the report and fetch the report data in apex. you can test this in developer console using the following code snippet.

notice here, the runReport() takes two params the report Id and a boolean. The boolean value passed here is ‘includeDetails’. This runs the report immediately with the report ID and if the boolean is set to true includes the report data in the reportResult. You can check the difference in the output JSON by setting the boolean true or false. 

The JSON output for my report is as follows :

Reports and Dashboard API

Since we won’t be executing the code snippet from developer console each time, let’s add it to a class which can be used as a controller to a lightning component. Create a new apex class and add the code below.

Step 3

Before handling the output JSON in the lightning component, let us take a look at how the data is structured in the JSON. The returned Reports.ReportResults has a different structure and format for Tabular, Summary and Matrix reports.

Step 4

Now let’s create the lightning component to render the data of the report as a chart on the UI.

Step 5

Let’s test the app and see the Reports and Dashboards Rest API in action.

Reports and Dashboards Rest API

You can read more about the Reports and Dashboards REST API here


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Lightning

LockerService for Secure Lightning Components

Lightning LockerService is a security architecture for Lightning components, that enhances security by isolating individual Lightning components in their own namespace. LockerService also promotes best practices that improve the supportability of your code by only Read more…

Lightning

Auto Style Existing Visualforce Pages with Lightning Experience Stylesheets

With the Winter 18 release, Salesforce has introduced Lightning Experience Stylesheets (Beta). This enables the visualforce page to be rendered with the look nad feel of lightning experience when viewed in Salesforce1 or in Lightning Read more…

Data Visualization

Display Salesforce Data on Google Map

Google Maps provide a cool feature where you can create your own map, import map features like places, lines, and shapes from spreadsheets/CSV, KML files, and some other supported formats. This feature can be used Read more…