2

I'm going to post this here even though I already have a workaround, but this is definitely a Salesforce Visualforce bug.

When adding an <apex:outputField> for the LastModifiedById field for the User record on a Visualforce Page. the page crashes with the following error:

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.LastModifiedBy**

It can be easily reproduced by creating a simple VF page:

<apex:page controller="testusercontroller">
    <apex:outputField value="{!me.LastmodifiedById}"/>
</apex:page>

And a simple controller

public with sharing class testusercontroller {
    public User getMe(){
        return [select CreatedById, LastmodifiedById from User where Id =:UserInfo.getUserId()];
    }
}

If you change the <apex:outputField> to display the CreatedById field, everything works fine. Only LastModifiedById crashes.


ANSWER

The workaround is to change the query to include LastModifedBy.Id and LastModifedBy.Name like this:

 public with sharing class testusercontroller {
     public User getMe(){
      return [select CreatedById, LastmodifiedBy.Id, LastmodifiedBy.Name from User where Id =:UserInfo.getUserId()];
     }
   }

Hopefully this helps others as I didn't find any reference to this anywhere.

  • Hi Alex, welcome to SFSE! Please take a brief moment to visit the help center and read How to Ask and How to Answer. If you want to post an ask-and-answer combo, the answer portion should be a separate answer post (below). – Adrian Larson Feb 2 '17 at 15:29
  • @alex-sartogo Is your query return value – Reshma Feb 3 '17 at 5:12
0

I think it's more because of the nature of outputfield. For reference fields (relationship fields), outputfield generates a link wherein the text is the record name and link points to record details page. So, by nature it needs both name and id of the linked record. I believe this is what is causing the error, which gets taken care of when you specifically add those additional fields in your query.

Can't say a bug, but error message may be modified for better problem description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.