3

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.

1 Answer 1

4

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>
3
  • I did what you suggested. And even though it shows the USD value for the currency, when I change the user default currency to EURO, the number is not getting converted into EURO, it still stays in the USD value. Commented Mar 25, 2014 at 13:48
  • you probably have to specifically set the currency for the new CustomObjA to get the same formatting. I updated my answer for this. Commented Mar 25, 2014 at 13:54
  • You were right. This worked. Thank you so much for your help. I am marking this as a solution. Commented Mar 25, 2014 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .