function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfadm sfadmsfadm sfadm 

Error: “SObject row was retrieved via SOQL without querying the requested field”

I'm getting this error while running this VF Page
 
Error: SObject row was retrieved via SOQL without querying the requested field: Task.LastModifiedById

An unexpected error has occurred. Your development organization has been notified.

VF Page:

<apex:page standardController="task" extensions="TaskHistoryController">
  <apex:form >
   <apex:pageBlock >
       <apex:pageMessages escape="false"/> 
    </apex:pageBlock>
  </apex:form>   
</apex:page>

Controller:
public with sharing class TaskHistoryController {



Task objTask;
String objTaskId;

public TaskHistoryController(ApexPages.StandardController controller) {
    objTask = (Task)controller.getRecord();
    Id taskId = objTask.Id;
    List<Task_Activity_History__c> listTaskActivityHistory = [SELECT Field_Name__c, Id, Old_Value__c, New_Value__c, LastModifiedById, LastModifiedDate FROM Task_Activity_History__c WHERE Task_ID__c = :taskId]; 
    Task_Activity_History__c taskHistory1 = listTaskActivityHistory.get(0);
    Task_Activity_History__c taskHistory2 = listTaskActivityHistory.get(1);
    String newValue1 = taskHistory1.New_Value__c;
    String newValue2 = taskHistory2.New_Value__c;
    String resultAll = buildTemplate(objTask);
    ApexPages.Message infoPage = new ApexPages.Message(ApexPages.Severity.INFO, resultAll);
    ApexPages.addMessage(infoPage);
}

public String buildTemplate(Task task) {
    Id lastModifiedBy = task.LastModifiedById;
    String userName = '';
    List<User> listOfUsers = [SELECT Id, IsActive, LastModifiedById, Name FROM User where Id = :lastModifiedBy];
    for(User user :listOfUsers) {
        userName = user.Name;
    }
    Id taskId = task.Id;
    List<Task_Activity_History__c> listTaskActivityHistory = [SELECT Field_Name__c, Id, Old_Value__c, New_Value__c, LastModifiedById, LastModifiedDate FROM Task_Activity_History__c WHERE Task_ID__c = :taskId];
    String pageHTMLContent = '<HTML><HEAD><STYLE>table.user_class, .user_class th, .user_class td { border: 1px solid black; padding: 10px; } table.user_class th { text-align: center; } </STYLE></HEAD><BODY><TABLE class="user_class">';
    pageHTMLContent += '<TR><TH>Comment Old Value</TH><TH>Comment New Value</TH><TH>Last Modified By</TH><TH>Last Modified Date</TH>';
    for(Task_Activity_History__c taskActivityHistory :listTaskActivityHistory) {
        String newValue1 = taskActivityHistory.Old_Value__c;
        String newValue2 = taskActivityHistory.New_Value__c;
        String lastModifiedById = taskActivityHistory.LastModifiedById;
        DateTime lastModifiedByDate = taskActivityHistory.LastModifiedDate;
        pageHTMLContent += '<TR>';
        pageHTMLContent += '<TD>' + newValue1 + '</TD><TD>' + newValue2 + '</TD><TD>' + userName + '</TD><TD>' + lastModifiedByDate + '</TD>';
        pageHTMLContent += '</TR>';
        System.debug('pageHTMLContent7 ' + pageHTMLContent);
    }
    pageHTMLContent += '</TABLE></BODY></HTML>';

    return pageHTMLContent;
}
Please advise an appropriate solution.
sfadm sfadmsfadm sfadm

Could you please advise how to populate the page with different values depending on the user name who last modified the task record?
User-added image