function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AussieBattlerAussieBattler 

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?
Ron HessRon Hess
in the controller, when you make your query

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

AussieBattlerAussieBattler
Hi Ron. Thanks for the response. I tried your suggestion but got an error when compiling (method does not exist). The relevant code I have used (where the error is occuring is):

Code:
public Personal_Details__c getpDetails() {
Personal_Details__c pDetails = [select Id, Name, Date_of_Birth__c from Personal_Details__c where eID__c = :eId];
if (pDetails.size() > 0) {
errmsg = 'yes';
} else {
errmsg = 'nope';
}
}

Do you have any idea what I have done wrong?

Cheers.

MyGodItsColdMyGodItsCold
Just a guess, did you include the limit clause to the select?
AussieBattlerAussieBattler
Hi. I have tested it with the limit clause and I get the same error. Apologies the example I posted didn't include it.
MyGodItsColdMyGodItsCold
What's the scope of :eId ? Just to eliminate that as a possible issue, I'd set it equal to a known eID & run it - knowing I'd get a hit & then again after setting it equal to an 18 char string I knew wasn't an eID.
AussieBattlerAussieBattler
The select clause works as I expect. I just want to have some sort of test in place to establish if a record exists or not (whether it brings back record or not). I could put a fudge in place on the database (i.e.: when the record is inserted update a flag on the database) but there must be an easier way to do this programmatically.

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.
Ron HessRon Hess
ok, i tested something, here is what i found

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

Ron HessRon Hess
change that to

Personal_Details__c[] pDetails = [select Id, Name, Date_of_Birth__c from Personal_Details__c where eID__c = :eId];

note the array [] notation, then you can use size()

Message Edited by Ron Hess on 11-05-2007 02:58 PM

AussieBattlerAussieBattler
Thanks for your help Ron. I still had problems implementing the array approach but the try catch statement was a simple solution in the end. Thanks for that.