+ Start a Discussion
ChrisMcLChrisMcL 

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.

Eg.

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

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

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

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

if(citizen.isEmpty()){

}

 




Message Edited by micwa on 06-26-2008 06:04 AM
SuperfellSuperfell
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
dptr1988dptr1988
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?


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