I'm trying to access the User field Email via the related Account Id. I use this query in my controller :

User currentUser = [SELECT Email FROM User WHERE AccountId=:accountId LIMIT 1];

But when I try to view the page it sends me an error :

List has no rows for assignment to SObject

The Account Id exist because when I use a qury like this it works :

Account acc = [SELECT Id, PersonEmail FROM Account WHERE Id = :accountId]

(but this is not what I want)


1 Answer 1


Just because the Account record exists does not mean any User records look up to it. A more appropriate methodology for querying records whose existence is not known is to use list assignment.

List<User> candidates = [SELECT Email FROM User WHERE ... LIMIT 1];
if (!candidates.isEmpty()) currentUser = candidates[0];

You must log in to answer this question.

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