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);
}
}
Attribution to: SSK
Possible Suggestion/Solution #1
While not a perfect solution you could:
- Create a custom Setting to store the record count by objectName
- Populate the Initial values for all records either manually or by a batch class
- Write a trigger on insert or delete for each object to update the value for each record (Utility class maybe)
- In your rest request, return the custom setting value for the object name
Given the limitations of number of rows returned you can customize for only those objects where you think the value will exceed 50,000 and revert to this method (saves you writing triggers for all objects), for others you can do the query. Doing it this way for all objects will speed up the request though.
Just one solution if the functionality is really needed.
Attribution to: Eric
Possible Suggestion/Solution #2
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.
@RestResource(urlMapping='/count/*')
global with sharing class GetCount{
@HttpGet
global static Integer doCount() {
RestRequest restreq = RestContext.request;
String objName = restreq.requestURI.split('/')[2];
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://na12.salesforce.com/services/data/v30.0/query/?q=SELECT+COUNT(Id)+FROM+' + objName);
req.setHeader('Authorization', 'Bearer '+ UserInfo.getSessionId());
req.setTimeout(60000);
Http http = new Http();
HTTPResponse res = http.send(req);
Map<String, Object> response = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
List<Object> records = (List<Object>) response.get('records');
return (Integer) ((Map<String, Object>) records[0]).get('expr0');
}
}
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.
Attribution to: Daniel Hoechst
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34741