1

I'm new to using the Reports classes in Apex and I want to retrieve the total number of opportunities referenced in a report.

I assume retrieving the record count is the simplest way to do this, but how does it work?

I have looked through the Reports Namespace section on https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_namespace_Reports.htm, but without examples I'm struggling.

3 Answers 3

1

This is what I found in ReportFact Class documentation:

ReportFact Methods The following are methods for ReportFact. All are instance methods.

  • getAggregates()

    Returns summary-level data for a report, including the record count.

  • getKey()

    Returns the unique identifier for a row or column grouping. This identifier can be used to index specific data values within each grouping.

0

I worked out the answer here:

Report r = [SELECT Id FROM Report WHERE Name = 'Report Name' LIMIT 1];
//run report
Reports.ReportResults results = Reports.ReportManager.runReport(r.Id, true);
// Get the fact map from the report results
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get('T!T');
Integer resultCount = factDetails.getRows().size();

Note that mine was an ungrouped report, so the default "T!T" might not be your grouping name.

0

You can use Code for getting the Count of Record from the Report, you just need to provide the report Name.

Report r = [SELECT Id FROM Report WHERE Name = 'Lead This Year' LIMIT 1];
// Running the Report
Reports.ReportResults results = Reports.ReportManager.runReport(r.Id, true);
// Get the fact map from the report results
System.debug('results ::'+results);
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get('T!T');
System.debug('factDetails ::'+factDetails);
Integer resultCount = factDetails.getRows().size();
System.debug('resultCount ::'+resultCount);
1
  • 1
    Thanks for your contribution. Instead of just a code snippet, it's better to supplement an answer with some explanation or links to documentation. It adds quality to the answer and helps understand how or why this answer is useful
    – Shamina
    Sep 14, 2022 at 3:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .