Website Loading ....

Read Data from Reports in Salesforce – Report API

3 min read

 

Report API is used to read the data from reports as part of customizing reports. Basically it is used when we want to perform some actions between two non related reports or want to display the reports on User Interface. There are so many scenarios but want to retrieve data from reports or get report summary values and perform some actions. for these scenarios Report API will be helpful.

Salesforce provide some standard methods to get the report data into apex. We will discuss the details here.

First take the Report and validate the data you want to retrieve from apex. Get report API name from Report properties to use it in Apex class.

Now create one Apex class to read / get report data. I creating the class called “GetApexReadData” class.

Apex Logic :

// Get Report using by writing SOQL quey

List <Report> reportData = [SELECT Id,DeveloperName FROM Report where DeveloperName = ‘Summary_Report’];

// Get Report Id by using above ReportData

String reportId = (String)reportData.get(0).get(‘Id’);

if you want to write it using Database.query. use like below.

Report reportData = Database.query(‘SELECT Id, Developername FROM Report WHERE Developername =: Summary_Report’);

Now we have report Id with us. we will retrieve report data from the report id.

We have to run the report to get the latest data / user based data. we can run the report using runReport method with reportid.

Reports.ReportResults results = Reports.ReportManager.runReport(reportId, true);

Next use getGroupingsDown() method used to row groupings, keys, and values. here we will have everything so get it from this method.

Reports.Dimension dim = results.getGroupingsDown();

if you want to get one values simple use index and get it. but want to bulkify or get multiple values use like below for loop.

list<String> keys = new list<String>();

Map<String,Object> GroupingValues = new Map<String,Object>();

for(reports.GroupingValue resu : dim.getGroupings()){
String factMapKey = resu.getKey() + ‘!T’;
keys.add(factMapKey);
GroupingValues.put(factMapKey,resu.getValue());
}

I have created one list and map to store the values.factMapKey will store the ids and getvalue store the values in map.

okay now this is the final step to get the summary values from using above keys.

for (String factMapKey : GroupingValues.keySet()) {
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);

Reports.SummaryValue summaryval = factDetails.getAggregates()[0];

// use factMapKey  as a key

// use summaryval  as a value
}

Above code helpful for reading the summary values but if you have a scenario like read/retrieve particular column values use the below logic. use same code from grouping values just use getAggregate method of index. count the column and

for (String factMapKey : GroupingValues.keySet()) {
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
if (factDetails != null) {
Reports.SummaryValue sumVals = factDetails.getAggregates()[3];
}
}

If you want to create report filters from Apex. Thinking about is it possible or not ?

Yes, It is possible. Find the below code snippet.

List<Reports.ReportFilter> filters = new List<Reports.ReportFilter>();
Reports.ReportFilter newFilter = new Reports.ReportFilter();
newFilter.setColumn(‘Account.Name’);
newFilter.setOperator(‘equals’);
newFilter.setValue(‘Test Account’);
newFilter.setColumn(‘Account.Name’);
newFilter.setOperator(‘equals’);
newFilter.setValue(‘Account Test name’);
filters.add(newFilter);

okay this is about reading data from apex class. Please check the next article for using this apex data in aura component / visualforce page and display chart. please check this page if you are looking for any salesforce services.

 

Sruvi IT

Sruvi IT Solutions

Expert in digital transformation, SEO, and enterprise technology. Helping businesses grow through innovative IT solutions since 2018.

Leave a Comment