You need to sign in to do that
Don't have an account?
AussieBattler
Test for null records when retrieving data
Hi. I am sure this is a simple question. I have a simple SELECT query in my controller which brings back one record, but occasionally there may be no records for it to bring back. Everything works fine, except I need to know whether the query has retrieved data or not. And I'm not sure how I can test whether my query has retrieved data or not?
I have tried various methods but none seem to work. Any suggestions or examples?
I have tried various methods but none seem to work. Any suggestions or examples?
this code
private void foo () {
User[] u = [select id from User where id = 'ff' ];
system.debug(u.size());
}
produces output :
*** Beginning Test 1: test.static testMethod void tester()
20071105214502.095:Class.test.tester: line 8, column 9: Output
20071105214502.095:Class.test.foo: line 5, column 9: 0
Message Edited by Ron Hess on 11-05-2007 01:46 PM
Code:
Do you have any idea what I have done wrong?
Cheers.
Ron suggested using size() which I added to my code but it gives me this new error. So I was wondering if I have added size() incorrectly or missed something. Perhaps Ron (if you are out there) could cast an eye over it?
Thanks for the help.
if i declare a single user object
User u
i need to limit to one record like this
User u = [select id from User where id = 'ff' limit 1];
but, since my id is bad, i expect no results
i get this
System.QueryException: List has no rows for assignment to SObject
so, you may use try {} catch () to determine if you get any records back from a query.
If you want to use size(), you must declare an array
User[] users = [select id from User ];
only then can you use size()
if ( users.size() > 0 )
Message Edited by Ron Hess on 11-05-2007 01:50 PM
note the array [] notation, then you can use size()
Message Edited by Ron Hess on 11-05-2007 02:58 PM