Skip to content

Instantly share code, notes, and snippets.

@leehildebrand
Last active February 3, 2017 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leehildebrand/85c76a1e4b40adc6578ac6048c7faad5 to your computer and use it in GitHub Desktop.
Save leehildebrand/85c76a1e4b40adc6578ac6048c7faad5 to your computer and use it in GitHub Desktop.
Return sObject records from a report on a very large object (records>200k)
// Call Schema to get the report metadata
Reports.ReportMetadata reportMd = Reports.ReportManager.describeReport(AnalyticAPISupport__c.getValues('DemoAnalyticAPI').Report_Id__c).getReportMetadata();
//Get the relevant filter in Reports.ReportFilter type variable and set it to the Account Number I'm looking for
Reports.ReportFilter filter = reportMd.getReportFilters()[0];
filter.setValue('12345');
// Run the report and store the rows in a Reports.ReportFactWithDetail object
// Make sure to use the three parameter version of the Reports.ReportManager class runReport() method to:
// 1. Run the report with the filter we just set and...
// 2. Return a Reports.ReportFactWithDetails object (as opposed to a summary ReportFactWithSummaries object)
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)Reports.ReportManager.runReport(AnalyticAPISupport__c.getValues('DemoAnalyticAPI').Report_Id__c, reportMd, true).getFactMap().get('T!T');
// Prepare empty array to populate
List<Account> accounts = new List<Account>();
// For each row the report returns, create an Account object and add to our array
for(Reports.ReportDetailRow detailRow : factDetails.getRows())
accounts.add(new Account(Id=Id.ValueOf(String.ValueOf(detailRow.getDataCells()[0].getLabel())),Name=String.ValueOf(detailRow.getDataCells()[1].getLabel())));
// What's in our accounts array?
system.debug(accounts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment