[SalesForce] Getting record count through WebService bypassing LIMITS

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.

@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.

Related Topic