I am getting the below error while writing a test class for the below class -

error -

System.QueryException: List has no rows for assignment to SObject

I am getting this error in line no 28 for apex class and line no 18 in test class.

As it is mentioning list has no rows but there is data which is retuning when run the query in workbench.


1 Answer 1


If you were to review the debug logs during the test you would see exactly what the issue is:

You are not setting the value of smartAccountID during the test.

Maybe add this to the constructor as ONE of the ways to help resolve the issue:

smartaccountId = con.getId();

Not sure what line 18 is in your test class but from what you posted there are NO possible ways for that error to be thrown as a result of the queries in the test class. Maybe line 18 is testOTMDelControllerExtension= new SMI_ManageInvestmentExtension (controller); in which case that is just part of the trace and what the line number was that called the method that threw the error in the apex class.

From what you posted none of the queries in your test class would throw that error, even if a record did not exist.

Additionally, is there really a need for the query? Since you are using a standard controller the record is already present so you could just use:

smartAccount = controller.getRecord();
  • hi eric i hv updated my entire code class. can u check now.. Aug 7, 2016 at 3:16
  • Same problem just different stripes. You are not setting the value of smartAccountID. Now with your updates it seems to come from the URL param of name. So set the pagereference in your test and populate the parameter of name with the required value.
    – Eric
    Aug 7, 2016 at 3:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .