I have a report that lists opportunities. The report is currently grouped by row on OpportunityId field (though, a simple tabular report without any grouping would work for our purposes, too, if that makes a solution easier).

I'm trying to grab all of OpportunityId values from the report (first column) and assign them to a List or Set so I can complete more logic on them later.

I'm stuck in the code below. While the code works, it only works if I'm able to specify the number of records in the report (ex: 15). I'm not sure how to get the results.getGroupingsDown() size/length so I can dynamically assign the iterator value to a variable value, rather than hardcoding it at 15. The apex fails if the iterator value exceeds the number of records in the report. If anyone has a better method to do the same function that would be great, I based this off the Salesforce dev guide example which uses a report with groupings, even though groupings aren't needed in my report. Thanks for any suggestions.

// Get the report ID
List <Report> reportList = [SELECT Id,DeveloperName FROM Report where 
    DeveloperName = 'Opportunity_Pipeline_sIu'];
String reportId = (String)reportList.get(0).get('Id');

// Stores the IDs pulled from the report.
Set <ID> reportIDs = new set <ID>();

// Run the report synchronously
Reports.reportResults results = Reports.ReportManager.runReport(reportId, true);

// Get the first down-grouping in the report
Reports.Dimension dim = results.getGroupingsDown();

// Iterate through results, add each ID to the set.
for (Integer i = 0; i < 15; i++) {
  Reports.GroupingValue groupingVal = dim.getGroupings()[i]; 

// Construct a fact map key, using the grouping key value
  String factMapKey = groupingVal.getKey() + '!T';

// Get the fact map from the report results
  Reports.ReportFactWithDetails factDetails = 


System.debug('reportIDs: ' + reportIDs); // Properly lists reports.
  • 1
    Is the report necessary? Is it possible to just query for the records you want and do the logic on the results from that? Commented Oct 10, 2019 at 0:07
  • @KrisGoncalves The report is necessary, unfortunately. The alternative would be to build out some type of LWC with a form to filter necessary records, but for this use case that would be less than optimal. Simply getting record ID's into a list would be simplest for the logic that is to follow.
    – learnsfdx
    Commented Oct 10, 2019 at 15:10

1 Answer 1


You should be able to get the length of the groupings by replacing your "15" with dim.getGroupings().size().

You may want to consider storing dim.getGroupings into a variable to make it easier to read your code (and probably a bit more efficient since you're calling getGroupings multiple times).

  • Thanks Chris, this worked! I had tried the .size() method but must have been trying it on the wrong variable. I'm now able to get an accurate count. Thanks! (PS I don't have enough reputation yet to upvote..sorry)
    – learnsfdx
    Commented Oct 11, 2019 at 13:07

You must log in to answer this question.

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