I have written the below web service class to return record count. But it is returning the following message "System.LimitException: Too many query rows: 50001". How could I overcome this limit?
@RestResource(urlMapping='/count/*')
global with sharing class GetCount{
@HttpGet
global static String doCount() {
RestRequest req = RestContext.request;
String objName = req.requestURI.split('/')[2];
String whereClause = EncodingUtil.urlDecode(req.requestURI.split('/')[3],'UTF-8');
return database.countQuery('SELECT COUNT() FROM ' + objName + ' WHERE ' + whereClause);
}
}
Best Answer
There is a way to do it, but it isn't pretty. You can have your Apex call the REST API and then parse the JSON to get the count. This works in my org with 1.5 million cases.
To use this you have to add your org in the remote site settings. You also shouldn't hard code the org instance in the class.