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

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. I’ve also run into this during code deployments or testing on records that dont have related records populated. If you add some defensive programming to your code you can safely handle zero results thus avoiding the problem for end users.

For example you shouldn’t do this:

Instead add some defensive programming tactics to your code:

 

Add Comment