Find your content:

Search form

You are here

Currency conversion for an Outputtext

 
Share

I am reading two currency fields from a custom object, displaying them and their difference on the vf page.

I am using outputfield to display my currency fields, so if the locale currency of the user is GBP or EURO, the currency is automatically converted from the default USD. The problem comes when displaying their difference. It is displayed in an outputtext (as I cannot use outputfield for a non sobject).

I dont want to go go through a whole lot of calculations and function calling to display this difference in the locale currency. So is there a way to do this efficiently?

PS: I tried using convertCurrency() in my SOQL query hoping to get the fields converted into locale currency and then subtracting the local currency values, but it is not working (I still get those fields in the default currency USD).

Something like this:

CustomObjA cust = [select convertCurrency(customCurrencyField1), convertCurrency(customCurrencyField2) from CustomObjA where Id = :recordId limit 1];
Decimal difference = cust.customCurrencyField1 - cust.customCurrencyField2;

Any ideas on how I can achieve this.


Attribution to: codeinprogress

Possible Suggestion/Solution #1

I usually solve this by mis-using some other currency field (e.g. create an extra CustomObjA, without ever saving it to the database) and putting the result of the calculation in that field. Then show that field on the screen.

in apex add a public variable for it:

public CustomObjA cust2 {get;set;}

and fill it whenever you need to do the calculation:

cust2 = new CustomObjA();
cust2.CurrencyIsoCode = cust.CurrencyIsoCode;
cust2.customCurrentyField1 = cust.customCurrencyField1 - cust.customCurrencyField2;

in visualforce (wrap in e.g. pageblockSectionItem since the label will be wrong):

<apex:pageBlockSectionItem>
   <apex:outputLabel value="Your custom label" />
   <apex:outputField value="{!cust2.customCurrencyField1}" />
<\apex:pageBlockSectionItem>

Attribution to: Guy Clairbois
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30819

My Block Status

My Block Content