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];
reportIDs.add(groupingVal.getValue().toString());
// 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 =
(Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
}
System.debug('reportIDs: ' + reportIDs); // Properly lists reports.