+ Start a Discussion
Amol BafnaAmol Bafna 

After running Apex test execution gets status as, “failed” with error messages "System.QueryException: List has no rows for assignment to SObject"

User-added image
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Amol,

The error “System.QueryException: List has no rows for assignment to SObject” normally occurs when you’re running a SOQL query that for some reason is giving zero results.

Can you check if any queries used in the apex class which is not returning any results.

If this solution helps, Please mark it as best answer.

Nitin ShyamnaniNitin Shyamnani
Hi Amol,

This type of error occurs when you are assigning a query result to variable directly and query is not returning any data.
To solve this issue, you need to fetch query data into a list and then you can check for its size and then you can assign to your variable

For example:

Contact conObj = new Contact();
List<Contact> conList = [SELECT Id, Name FROM Contact WHERE Id =: conId];
if(conList != NULL && conList.size() > 0){

conObj = conList[0];


If still facing issue, please share code screenshots.

mukesh guptamukesh gupta
Hi Amol,

Below link will help you for easy understanding :-


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.