Formatting Salesforce OutputText

Quite recently I came across an interesting forum post by XactiumBen that mentioned some awesomesauce abilities of params in outputTexts. I’ve written an article detailing the basics of this functionality before, and thought I’d document these advanced capabilities too. For anyone interested you can read Part 1 here.

Now I’m sure some of you thought that simply being able to use dynamic custom labels was pretty smashing in it’s own right, but you ain’t seen nothing yet. Imagine a world where you could not only use dynamic custom labels, but were also able to use decision structures in those labels. That’s the world we live in buddy!

Have a gander at this code:

<apex:outputText value=”On {0, Date}  I had {1, choice, 0#no|500#some|500<{1, Number, Currency} monies.>

<apex:param value=”{! MyObject__c.CreatedDate}” />

<apex:param value=”{!MyObject__c.AmountOfMonies__c}” />

</apex:outputText>

Let’s say the createdDate is 14/11/2009 and that amountOfMonies__c is 0, then the outputText would display as

On 14/11/2009 I had no monies.

If AmountOfMonies__c was 500 or 700, the outputText would display as

On 14/11/2009 I had some monies.

or

On 14/11/2009 I had $700 monies.

Holy Mackerel! What we have here is not only the ability to parametrise custom labels, but also the capacity to format and make wording decisions based on said parameter values.

That said we can summarise the syntax more or less as:

{ ArgumentIndex } // or
{ ArgumentIndex , FormatType } // or
{ ArgumentIndex , FormatType , FormatStyle }

Where argument index is the integer describing the position of the parameter(in the example the Date parameter is as ArgumentIndex 0), and FormatType and FormatStyle can be summarised thus,

Format TypeFormat StyleSubformat Created
(none)(none)null
number(none)NumberFormat.getInstance(getLocale())
integerNumberFormat.getIntegerInstance(getLocale())
currencyNumberFormat.getCurrencyInstance(getLocale())
percentNumberFormat.getPercentInstance(getLocale())
SubformatPatternnew DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))
date(none)DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
shortDateFormat.getDateInstance(DateFormat.SHORT, getLocale())
mediumDateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
longDateFormat.getDateInstance(DateFormat.LONG, getLocale())
fullDateFormat.getDateInstance(DateFormat.FULL, getLocale())
SubformatPatternnew SimpleDateFormat(subformatPattern, getLocale())
time(none)DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
shortDateFormat.getTimeInstance(DateFormat.SHORT, getLocale())
mediumDateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
longDateFormat.getTimeInstance(DateFormat.LONG, getLocale())
fullDateFormat.getTimeInstance(DateFormat.FULL, getLocale())
SubformatPatternnew SimpleDateFormat(subformatPattern, getLocale())
choiceSubformatPatternnew ChoiceFormat(subformatPattern)

It seems that this functionality is wrapping similar Java capabilities, and as a result you can find quite a few more tidbits in the JavaDoc.

One issue that has been encountered is that formatting numbers as currencies only ever outputs Dollar values. If I were to hazard a guess I’d say that the servers running the Apex code(which is wrapping the Java code) have their system locale set to US.

8 thoughts on “Formatting Salesforce OutputText”

    • A little tardy on the reply here, sorry Scott. I’ve dug around and it looks like this is Java functionality that’s been left in Apex/Visualforce so I think it can’t be invoked from JS.

      Reply
    • A little tardy on the reply here, sorry Scott. I’ve dug around and it looks like this is Java functionality that’s been left in Apex/Visualforce so I think it can’t be invoked from JS.

      Reply

Leave a Comment