Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am getting "Error System.QueryException: List has no rows for assignment to SObject".

Line 1: Account ac = [select id from Account where name='testaccount' limit 1]
Line 2: if(ac != null) {
          //do something
        }

I got the above error in line 2 if there is no record.

Any help?

share|improve this question
add comment

3 Answers

If you want to assign query results to a single sObject instance, you must guarantee that the query will return exactly 1 record. Not zero, not 2 or more, exactly 1.

If you can't, there are two options. First is assign to a list and test the size:

List<Account> accts = [select name from Account where id = ???]; 
if (accts.size() == 1) {
   ...do something here...
}

Alternatively, you now know the exception you will recieve, so you can use try/catch.

Account myAcct;
try {
myAcct = [select name from Account where id = ???];
} catch (SearchException e) {
myAcct = new Account(); //or do something else, perhaps
}

I like this when I either need the retrieved record, or a new empty record. I commonly use this pattern in my Visualforce page controller constructor methods.

Note: I used the where Id = where clause as this implicitly returns a maximum of 1 record on the Force.com platform, but does not guarantee a record be returned in all cases.

share|improve this answer
add comment

I dont know whether this is a best practice or not but, whenever i have encountered such error or rather always, i use a List instead of the object itself. and then later on in the code, check whether the list has any elements present in it or not.

So in your case, you could do something like:

List<Account> accounts = new List<Account>();accounts = [select id from Account where name='testaccount' limit 1] // Doing so does not throw any error

And then somewhere down in your code, where you are trying to access the query result, add

if(!accounts.isEmpty()){ // do something }

and since you are going to have only one record, you could simply use

accounts[0].Id to reference the Id field or for some other operation

share|improve this answer
    
Thanks for your answer but i want to use object instead of list. is it possible Thanks sfdc –  sfdc Jan 30 '13 at 6:54
    
well i dont know about that. I am not sure but then you could try this Account acc; for(Account act: [select id from Account where name='testaccount' limit 1){acc = act}; –  Shailesh Deshpande Jan 30 '13 at 7:06
add comment
List<Account> ac = [select id from Account where name='testaccount' limit 1];
if(ac.size() >0) { 
    //Do some thing
}

Check with size assigning to list.

share|improve this answer
    
thanks for your answer. but i don't want use list. insted of list i want to use object. –  sfdc Jan 30 '13 at 6:52
    
I don't know weather there is a method to check the obejct is empty or not. if the query do not return a row you should assign it to an array and to check it has returned a row or not. –  Ugesh Gali Jan 30 '13 at 7:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.