You need to sign in to do that
Don't have an account?
Akhil 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);
}
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);
}
Please check with below link which might help you further with the above issue.
- https://salesforce.stackexchange.com/questions/158516/get-more-than-2000-record-from-report-in-apex
- https://developer.salesforce.com/forums/?id=906F0000000D6aZIAS
Please let us know if this helps.Kindly mark this as solved if it's resolved.
Thanks,
Nagendra