Pages

Tuesday, January 3, 2012

VF Templates: Formatted Merge Values (dates)

Just like in the previous post, another type of field that you might not receive your expected data format are date fields.
My record said: 

This is the VF code I was using:
{!relatedTo.myDateField__c}

The resulting text displayed in an email was:

That's not too pretty.
For date fields, you can extract individual pieces of the date, including the month, year, and date.  Once you pull those pieces out, you can separate them with a forward slash (“/”) and then it will look more like the date field you were hoping for.

I'm hoping for something that looks like "1/3/2012."

To retrieve the "1" for the month:

{!month(relatedto.Distribution_Form_Received_Date__c)}

To retrieve the "3" for the date:


{!day(relatedto.Distribution_Form_Received_Date__c)}


To retrieve the year "2012":

{!year(relatedto.Distribution_Form_Received_Date__c)}


Put them all together with those forward slashes:
{!month(relatedto.Distribution_Form_Received_Date__c)}/{!day(relatedto.Distribution_Form_Received_Date__c)}/{!year(relatedto.Distribution_Form_Received_Date__c)}


With that little bit of magic, your end result will be:

Monday, January 2, 2012

VF Templates: Formatted Merge Values (currency)

Once in a while, I'll run across something that I've forgotten all about.  Today, I was working with a Visualforce e-mail template that contained a currency field as one of the merge fields.

My record said:  $99.00
I was pulling it in with just this VF code:

{!relatedTo.myCurrencyField__c}

Just doing that resulted in the template displaying:
Not quite what I was looking for.  What I ended up doing was throwing my merge data into a pre-formatted apex:outputText field, using an apex:param:

<apex:outputText value="{0,number,$###,###,##0.00}">
    <apex:param value="{!relatedTo.myCurrencyField__c}" />
</apex:outputText>

This will format the dollar amount up to $999,999,999.99.  The 0's are there just incase your amount is not a full dollar ($0.89) or an amount that ends in 0 ($25.40).  Otherwise, those values are truncated ($.89 and $25.4 respectively).

Now the template looks like this:





To see more of what you can do, check out:  Java MessageFormat