+ Start a Discussion
Akhil MehraAkhil Mehra 

How to retrieve more than 2000 records (or say 48000 records ) from salesforce report in apex code.

Hi , 
I have a report which have 48000  records  , when i run a report using synchronous Approach in apex code , it will only return 2000 records  so in order to overcome this problem i have used filter in report but after returning 4000 records it throw exception " FATAL_ERROR System.LimitException: reports:Too many query rows: 50001 "  . (I can't Use Asynch Approach as i am using this class inside batch class).
In order to Apply Filter i am using Autonumber field and sort them in ascending order, Please have a look to my code and help me to figure out the problem , Thanks to All In Advance.

String reportId='00Of1000004jG5h'; // Report Id
Integer filterNumber=0; // Set Lower Row Limit For Filter 
boolean getAllData=False;
integer count=0;

while(getAllData==false) {
Reports.ReportDescribeResult describe = Reports.ReportManager.describeReport(reportId); // describe report metadata
            Reports.ReportMetadata reportMd = describe.getReportMetadata();
                string b =reportMd.getDetailColumns()[0]; //get Filtered Column From Report , it is Autonumber Field
                String column1=b;
                Integer value1=filterNumber;
                String operator1='greaterOrEqual';
              Reports.ReportFilter filter = reportMd.getReportFilters()[0];// get Filter and Update filter values in apex
             Integer value2=value1+2000; // Upper Limit for Second Filter
            String operator2='lessOrEqual';
             Reports.ReportFilter filter1 =reportMd.getReportFilters()[0]; // Second Filter to report on same column 
           // Run a report synchronously
            Reports.reportResults ActiveISOContacts = Reports.ReportManager.runReport(reportId,reportMd, true);
           system.debug(ActiveISOContacts.getAllData());//this will return true and false .

           // Get the fact map from the report results
            Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)ActiveISOContacts.getFactMap().get('T!T');
            //create a list of report rows and populate it with the result rows from fact map
            List<Reports.ReportDetailRow> reportRows = factDetails.getRows();
            filterNumber =filterNumber+reportRows.size();

NagendraNagendra (Salesforce Developers) 
Hi Akhil,

Please check with below link which might help you further with the above issue. Please let us know if this helps.

Kindly mark this as solved if it's resolved.

Akhil MehraAkhil Mehra
No sir i am using same approach but i doesn't work for me.
Björn Wiberg 4Björn Wiberg 4
Setup Analytic Snapshot and then read report results using APEX? The number of records will be the same as the aggregated result? If you need to retrieve detail records, reports aren't the right way to go. In that case you could write Scheduled APEX running a Flow, and if that's not enough go for SOQL query in APEX.
Björn Wiberg 4Björn Wiberg 4