Activity › Forums › Salesforce® Discussions › System.Query Exception: List has no rows for assignment to s-object?
-
System.Query Exception: List has no rows for assignment to s-object?
Posted by Prachi on September 10, 2018 at 2:35 PMSystem.Query Exception: List has no rows for assignment to s-object? How to resolve it?
Avnish Yadav replied 7 years, 9 months ago 4 Members · 3 Replies -
3 Replies
-
Hi,
The exception is because of you are not getting any record in your SOQL query which meet the where criteria so it’s give error. When attempting to fetch a single record from the database it is easy to run into the above exception. To avoid such exception use try-catch box like below :
MyObject__c obj;
try{
obj = [SELECT id FROM MyObject__c WHERE name=:previouslyDefinedVar];
} catch(System.QueryException e){
// Perform logic here
}
Hope this helps!
- [adinserter block='9']
-
Hi
It means your SOQL didn’t return any results. You can wrap it in a try/catch statement to handle this gracefully.
This error occurs when query doesn’t return any rows.
For e.g.:- Account a = [select id, name from Account where name = ‘test’];
Instead of using this we should use like that:-
List<Account> lstAccount = [select id, name from Account where name = ‘test’];
if (lstAccount.size() > 0)
{
Account a = lstAccount.get(0);
}Thanks
-
Hello Prachi,
Well, this type of error shown when your SOQL didn’t return any results. You can wrap it in a try/catch statement to handle this gracefully.
For more information, kindly share your code.
Thanks.
Log In to reply.