The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Formatting Salesforce OutputText

with 3 comments

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} there {1, Choice, 0#were no employees|1#was one employee|1<were {1, Number, Currency} employees}”>    <apex:param value=”{!Account.CreatedDate}” />
<apex:param value=”{!Account.NumberOfEmployees}” />
</apex:outputText>

<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 Type Format Style Subformat Created
(none) (none) null
number (none) NumberFormat.getInstance(getLocale())
integer NumberFormat.getIntegerInstance(getLocale())
currency NumberFormat.getCurrencyInstance(getLocale())
percent NumberFormat.getPercentInstance(getLocale())
SubformatPattern new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))
date (none) DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getDateInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getDateInstance(DateFormat.LONG, getLocale())
full DateFormat.getDateInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
time (none) DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getTimeInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getTimeInstance(DateFormat.LONG, getLocale())
full DateFormat.getTimeInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
choice SubformatPattern new 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.

Advertisements

Written by Wes

July 24, 2009 at 4:25 pm

3 Responses

Subscribe to comments with RSS.

  1. […] Formatting Salesforce OutputText @wesnolte Advanced capabilities of params and outputTexts. […]

  2. Awesome 2 part series! Now, what about populating the page with JavaScript yet having the translation occur from the string you produced in JavaScript. Possible?

    Scott Hemmeter (@hemmeter)

    September 24, 2012 at 8:30 pm

    • 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.

      Wes

      October 12, 2012 at 11:07 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: