4

To test some apex functionality, we wanted to update the conversion rates of currencies in a test class. It doesn't work since DML operations aren't allowed on CurrencyType via apex.

Here is more information about the whole sitation: A custom object holds a certain amount and a currency. There is another field which holds the amount in euros. We wrote a (before insert/before update) trigger on the custom object to calculate the amount in euros based on the conversion rates of the currencies and the 2 fields on the custom object.

Every month the conversion rates will be adjusted, so the euro field also needs to be updated. We wrote a batch process to accomplish this. Now we want to write a test class for the batch process. To write a good test for the functionality, we need to update the conversion rates of the currencies, but it seems it isn't possible through apex.

Does anyone know a solution for this? Many thanks

2

Are you trying to insert a new CurrencyType or update an existing one?

Either way, based on Update ConversionRates for CurrencyType from Apex, it doesn't appear you can update the ConversionRate via Apex. CurrencyType is also listed in the sObjects That Don’t Support DML Operations.

Its less desirable, but you can use Test.isRunningTest() to return conversion rates for testing. I.e. wrap all your code that accesses the conversionRate in a single class. Then, in this class, return the actual value unless Test.isRunningTest() is true. In which case you can return a mock value to simulate changes during testing.

| improve this answer | |
  • I am trying to update an existing CurrencyType. As you mentioned, it doesn't seem to be possible to update the ConversionRate through apex. I was just wondering if there is a workaround for it. At the moment it means I am not able to write a good test class for the batch process. – svenf Sep 30 '13 at 6:12
  • My only idea currently is to return fake data with Test.isRunningTest(). There appears to be a lot happening behind the scenes when you edit a conversion rate - See ("Changing conversion rates causes a mass recalculation of roll-up summary fields")[help.salesforce.com/apex/…. This is probably part of the reason you can't make the change via apex. – Daniel Ballinger Sep 30 '13 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.