5

This one had me banging my head against the wall for a while today. It appears that the DATEVALUE() function in Visualforce is unable to parse a Date/Time field after is has been used in an apex:outputField component.

Consider these two examples:

This VF page saves fine:

<apex:page standardController="Account">
{!DATEVALUE(Account.CreatedDate)}
</apex:page>

enter image description here

If we utilize the apex:outputField component to render the CreatedDate field first, DATEVALUE() is no longer able to recognize it as a valid expression.

<apex:page standardController="Account">
  <apex:outputField value="{!Account.CreatedDate}" />
  {!DATEVALUE(Account.CreatedDate)}
</apex:page>

enter image description here

In my case I was trying to use apex:outputField to display a nicely formatted date for the user and then use DATEVALUE() to construct some url parameters for a report link. Am I crazy for thinking this should work?

2 Answers 2

2

That is a strange one! I have a workaround for you: use an apex:variable to store the datevalue for later use:

<apex:page standardController="Account">
  <apex:variable value="{!DATEVALUE(Account.CreatedDate)}" var="dateval"/>
  <apex:outputField value="{!Account.CreatedDate}" />

  <br/>
  -----
  <br/>
  <span>{!dateval}</span>
</apex:page>

Unfortunately I don't know why this strange bug occurs - perhaps someone else will be able to illuminate you.

2

DATEVALUE expects the input of 'YYYY-MM-DD' format as a String. The documentation also says that it accepts DateTime, though I'm not sure why that doesn't work. Another alternative is rendering a Date or DateTime field using the following syntax using outputText with param:

<apex:outputText value="{0, date, dd'/'MM'/'yyyy}">
    <apex:param value="{!Account.CreatedDate}" />
</apex:outputText>

You must log in to answer this question.

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