Contents[Hide]

Equivalent of Created By (or Last Modified By) field in Visualforce.

We want to add Created By field to Visualfoce page which looks exactly as Salesforce Standard one, with date formatted to local user settings and adjusted by time zone (remember that as it is tricky):

Salesforce Standard Cretedby field

You may be surprised when you want to display Created By on Visualforce Page with

<apex:outputField value="{!my_custom_object__c.CreatedBy"/> 

it leads to error:

Expression value does not resolve to a field
Error is in expression '{!my_custom_object__c.CreatedBy}' in component <apex:outputField> in page my_vf_page

This is not enough as on custom objects it is LookUp field to User object.
You need to add field name from user object. In this case it will be "Name" as shown below.

<apex:outputField value="{!my_custom_object__c.CreatedBy.Name"/> 

But this displays name only which is not a link. We are missing the date too.

We need some improvements.

In SF standard component link pint to user ID: https://cs8.salesforce.com/005D0000001ARTa
We need to modify our code then to:

<apex:pageBlockSectionItem >
    <apex:outputLabel for="CreatedBy" value="Created By" />         
    <apex:commandLink id="CreatedBy" action="/{!My_Custom_Object__c.CreatedBy.Id}" value="{!My_Custom_Object__c.CreatedBy.Name}" />
</apex:pageBlockSectionItem>

What this code do:
- apex:pageBlockSectionItem - wrap all code inside this tag to one filed in page layout
- apex:outputLabel - define a label for command link.
- apex:commandLink - create a link to "/{!My_Custom_Object__c.CreatedBy.Id}" with a name "{!My_Custom_Object__c.CreatedBy.Name}"

As result we have click able link:

Date and time.

Now things become more complicated.

Create date is stored in "CreatedDate" filed so we need to add one more line:

<apex:pageBlockSectionItem >
    <apex:outputLabel for="CreatedBy" value="Created By" />         
    <apex:commandLink id="CreatedBy" action="/{!My_Custom_Object__c.CreatedBy.Id}" value="{!My_Custom_Object__c.CreatedBy.Name}" />
    <apex:outputText value="{!My_Custom_Object__c.CreatedDate}" /> 
</apex:pageBlockSectionItem>

but it can't be placed just under apex:commandLink as shown above because this leads to an error:

Error: <apex:pageBlockSectionItem> may have no more than 2 child components    

We need to group those two elements to be shown as one. To solve that we wrap them in to apex:outputPanel:

<apex:pageBlockSectionItem >
    <apex:outputLabel for="CreatedBy" value="Created By" />   
    <apex:outputPanel>
        <apex:commandLink id="CreatedBy" action="/{!My_Custom_Object__c.CreatedBy.Id}" value="{!My_Custom_Object__c.CreatedBy.Name}" />
        <apex:outputText value="{!My_Custom_Object__c.CreatedDate}" /> 
    </apex:outputPanel>
</apex:pageBlockSectionItem>

Finally the date is displayed, but something is wrong with date format.

It is raw unformatted datetime string not depended on user locale settings and time zone, we can not accept that.

Little trick to format datetime to local settings, which saves you hours of searching.

I was searching a lot on SF developer forum, even found nice component written by Greg Hacic, and a lot of unanswered topics.
Finally got my own solution which is very simple, it is enough to add space before Merge field.

Final code:

<apex:pageBlockSectionItem >
    <apex:outputLabel for="CreatedBy" value="Created By" />   
    <apex:outputPanel >
        <apex:commandLink id="CreatedBy" action="/{!My_Custom_Object__c.CreatedBy.Id}" value="{!My_Custom_Object__c.CreatedBy.Name}" />
        <apex:outputText value=", {!My_Custom_Object__c.CreatedDate}" /> 
    </apex:outputPanel>
</apex:pageBlockSectionItem>

That's it, adding this one blank space before (also I have added comma to get similar look as SF) datetime string is formatted to user local settings and offset by timezone. Very simple to do, very hard to find out!

Our Visualforce output:

Salesforce orginal component:

You may want to change your Locale and TIme zone settings in your profile to check if it works for you.

Custom date, time and number formatting.

If you need your own custom date format you may do that using apex:outputText please refer SF documentation here.
String formatting folows the rules of MessageFormat class in Java which is documented here.

   
© Paweł Woźniak