I was modifying a test class for a trigger when I bumped into this error message -
"List has no rows for assignement to SObject". This means that an object instance that is to be assigned a value from a SOQL query does not get assigned, i.e. the SOQL query return no value.
My case was something like this:
Opportunity opp = [select id, StageName where id = :someId] |
If the SOQL query for some reason would not a return a result row the error message would be raised. A way to solve this is to assign the result of the SOQL-query to an Opportunity array instead.
Opportunity[] opps = [select id, StageName where id = :someId] |
and then check with conditions:
if (opps.size() > 0) { /* At least one row has been returned from the SOQL query If we for example are only expecting one opportunity as result then get a handle on it like this */ opps[0].CloseDate = date.today(); } else { // Query was empty }
update opp[0]; |
This way no error is raised.