function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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
             
            filter.setColumn(column1);
            filter.setValue(string.valueOf(value1));
            filter.setOperator(operator1);
             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 
            filter1.setColumn(column1);
            filter1.setValue(string.valueOf(value2));
            filter1.setOperator(operator2);
    
        
           // Run a report synchronously
            Reports.reportResults ActiveISOContacts = Reports.ReportManager.runReport(reportId,reportMd, true);
            getAllData=ActiveISOContacts.getAllData();
           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();
            count++;
                system.debug(filterNumber);
    system.debug(count);
    }           


 
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.

Thanks,
Nagendra
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
https://help.salesforce.com/articleView?id=data_about_analytic_snap.htm&type=5