Displaying Currencies in a SFDC Email Template

February 22, 2010 Appirio

Glenn Weinstein

A few weeks ago I posted here about displaying currencies in a SFDC formula field or Visualforce page.  The key challenge is preserving the comma separators and ensuring exactly 2 decimal places, so that, for example, you see USD 2,978.10 and not USD 2978.1.  To do this, I wrote an elaborate formula, which could be used in defining a custom field to go alongside your original currency field.

I’ve now extended the concept in two important ways:

  • I want to display currencies in SFDC email templates (specifically Visualforce templates).  Emails just don’t look right when you are talking about currency and display “2978.1”. 
  • I don’t want to have to add a custom formula field for every currency field.  I’d rather have some kind of generic solution that I can use ad-hoc when creating new email templates, without having to modify the data model.

I actually wish SFDC had a built-in formula function for this, like Javascript’s toFixed() function, that let you specify exactly how many decimal places to display.  But until they add something like that, we’ll need to resort to some trickery.

The approach is actually fairly simple – wrap my elaborate SFDC formula into a Visualforce custom component.  I’ve named it asCurrency.  To use it in a Visualforce email template, all I have to do is wrap my currency field in an asCurrency tag:

That’s reasonably clean.  I’m happy enough for now!

Here’s the source code for the Visualforce custom component:


    description=”The value to be rendered as currency.”  

    amount >= 1000000,
    TEXT(FLOOR(amount / 1000000)) & “,”,
    “”) &
    amount >= 1000,
    RIGHT(TEXT(FLOOR(amount / 1000)), 3) & “,”,
    “”) &
  RIGHT(TEXT(FLOOR(amount)), 3) & “.” &
    MOD(amount, 1) * 100     “0” & TEXT(ROUND(MOD(amount, 1), 2) * 100),
    TEXT(MIN(ROUND(MOD(amount, 1), 2) * 100, 99))

Be sure to name your custom component “asCurrency”.  Note, we need the access=”global” bit (in the top-level apex:component tag) in order for our component to be available to Visualforce email templates.

Previous Article
Relationship Lookup Objects in Triggers are NULL?

Jeff Douglas I see this question once in awhile on the Salesforce.com message boards so I thought I’d put s...

Next Article
Displaying Currencies In a SFDC Formula Field or Visualforce
Displaying Currencies In a SFDC Formula Field or Visualforce

Glenn Weinstein I wanted to put a Formula field on the PSE Project detail page to show a red or green flag ...