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
Christopher PezzaChristopher Pezza 

How to get the User name in a list and display it in visual force in a soql query

I want to out put the Created By Name instead of the ID but don't know how to query the Users Name since __r doesnt work on the CreatedById Field. And then output the Name of the user on the repeated output of code.
VisualForce Page Code:

<apex:repeat value="{!customerHealth.Histories}" var="hh">
    <tr>
       <td><apex:outputText value="{0, date, EE MMMM dd, YYYY}"><apex:param value="{!hh.CreatedDate}"/></apex:outputText></td>
       <td><apex:outputText value="{!hh.CreatedById}"></apex:outputText></td>
       <apex:outputPanel rendered = "{!hh.Field = 'created'}">
           <td><apex:outputText value="{!hh.Field}"></apex:outputText></td>
       </apex:outputPanel>
       <apex:outputPanel rendered = "{!hh.Field != 'created'}">
           <td>Changed <strong><apex:outputText value="{!hh.Field}"></apex:outputText></strong> to  <strong><apex:outputText value="{!hh.NewValue}"></apex:outputText></strong></td>
       </apex:outputPanel>
   </tr>
</apex:repeat>

Apex Class Code:

List<Customer_Health__c> customerHealths = new List<Customer_Health__c>([SELECT Name, (SELECT NewValue, Field, CreatedById, CreatedDate FROM Histories) FROM Customer_Health__c WHERE Id= :CHidNum]);
customerHealth = customerHealths.get(0);
Best Answer chosen by Christopher Pezza
Sunil02KumarSunil02Kumar
Hi Christopher,

Use Createdby.name and you will get user name instead of id. So your query should be:

List<Customer_Health__c> customerHealths = new List<Customer_Health__c>([SELECT Name, (SELECT NewValue, Field, CreatedById,Createdby.name, CreatedDate FROM Histories) FROM Customer_Health__c WHERE Id= :CHidNum]);

Hope this will help you.
[If it solves your problem, please mark it as solution]

Thanks,
Sunil Kumar