+ Start a Discussion
Peter KayePeter Kaye 

Count number of records in SOQL query

$query = "SELECT count() from Normal_Service__c ";

$response = $mySforceConnection->countQuery($query);

print_r ($response);

$mySforceConnection returns a value to show a connection to the database. 
Any ideas why this code fails to fill the variable $response with the number of records in the custom object Normal_Service__c ?   Thanks.
Change your query to SELECT COUNT(Id) FROM Normal_Service__c
If you just wanted to know the number of records in a Normal_Service__c  Object then I would suggest something alog following lines...
AggregateResult[] arList = [select count(id) from Normal_Service__c ];
Peter KayePeter Kaye
Thanks for these suggestions.

I could not get bainesy's suggestion to work - using his SQL with countQuery produced a blank screen ( no error ).  I am looking further at Harshil's code - I haven't yet managed to translate it to valid php.

This is what worked:
$query = "SELECT COUNT()  FROM Normal_Service__c";

$response = $mySforceConnection->query($query);

echo "No. of records = ".$response->size;

Just a normal query ( or queryAll) !   Interestingly  replacing  COUNT() with COUNT(Id) gives the record count as 1.  There's lots I don't get here, not the least why countQuery doesn't work, and why putting a field name in COUNT() gives the wrong result.  I'll come back to these and post again if I can get any clarification.  Thanks again for your input on this.

David HalesDavid Hales
HI ,

You need to Change your Code to 
$query = "SELECT count(id) from Normal_Service__c ";

$response = $mySforceConnection->countQuery($query);

print_r ($response);

David Hales (1058)