Line: 5, Column: 1 System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.UserRole

In the debug statement if I add UserRole.DeveloperName i get the above error How to print UserName and UserRole

   List<User> usrlst = [SELECT Id, Name FROM User WHERE UserRole.DeveloperName LIKE : '%CSR%' and IsActive = True];

   for (User u: usrlst)  {
          system.debug ('The CSR name and role:'  + u.Name );

1 Answer 1


The error is pretty straightforward. If you want to reference a field from a queried record, you need to include that field in the SELECT clause.

In this case, that means including UserRole.DeveloperName in the SELECT.

You must log in to answer this question.

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