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
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.

Thanks,
 
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 :-

https://acesalesforce.com/system-queryexception-list-has-no-rows-for-assignment-to-sobject/#:~:text=The%20error%20%E2%80%9CList%20has%20no,returned%20to%20a%20single%20variable.

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

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

Thanks
Mukesh