[SalesForce] Setting a Multicurrency field in Apex

I have a currency field on my Opportunity object, in a multicurrency enabled org, and I have a numerical field in a child object of it, which holds an "always" GBP financial value.

I then have a trigger which at a certain moment in time needs to copy the numerical field into the Opportunity..

When I do this, the raw numerical value is dumped into the Opportunity, and then displayed as is against the Opportunity Currency ISO code.

eg. The child has a numerical value of 600.50 – which is known asolutely to be GBP, but when this is copied into an Opportunity whose currency setting is USD, it shows something like

600.50 USD (390.53 GBP)

(ths GBP bit is because of my locale settings, users in other currencies will see even more random conversions)

So my question is How do I indicate when I set a value into a multicurrency field that it is in fact in GBP – or – how do I determine what Curreny it is expecting, and convert my GBP value into it first? (even though in the second scenario, the display is just going to covert it right back in the bit in the brackets!)

Best Answer

If an org has multicurrency enabled, the standard field CurrencyISOCode is available on every object. It will default to a user's default currency. You can set it to any of the enabled currencies for your org. Enabled currencies are found in Setup > Company Profile > Manage Currencies.

So if you have an always-GBP value, you need to check Opportunity.CurrencyISOCode and if it isn't GBP, find the exchange rate for your currency in the CurrencyType object and read its ConversionRate.

For example:

SELECT Id, ConversionRate 
FROM CurrencyType 
WHERE IsoCode = :Opportunity.CurrencyISOCode

If your org has Advanced Currency Management enabled, exchange rates are stored in the DatedConversionRate object, and have a validity date range. For example:

SELECT Id, ConversionRate
FROM DatedConversionRate
WHERE IsoCode = :Opportunity.CurrencyISOCode
AND   StartDate <= :Opportunity.CloseDate
AND   NextStartDate > :Opportunity.CloseDate

You can use any date you want to get the dated exchange rate, but as @crop1645 pointed out, the default that Salesforce uses for Opportunity Amount is CloseDate, so you would be consistent if you use CloseDate as well.

Related Topic