+ Start a Discussion

single SOQL query result check for zero returned records?

Before I use the results in a SOQL single query. How do I check for if the query returned 0 rows? (The size() method is not available in queries where I only expect to get at most one record.


Account citizen = [select Id, Name from Account where Name = 'Citizen' limit 1];

if (?citizen is not empty?) {
i'm not sure but can't you do that:

if( citizen != null){
  //do sth.

 But what you can definitely do:
List<Object> citizen = [Select .... from Object...];




Message Edited by micwa on 06-26-2008 06:04 AM
If you bind a query result to a single object, and the query returns anything other than a single row, the query will throw an exception
So what is the point in binding the query result to a single object, when nearly every query has a (small) chance of returning 0 records? In the example above, there is a small chance that the 'Account' table could be empty ( even with out the " where Name = 'Citizen' " clause) ?  correct??

 Or does SalesForce not operate unless there is some basic, default rows in the Accounts, Contacts, etc tables?

If your data and your query, only you know if it'll always return 1 row, or could possibly return 0 rows, choose accordingly.