[SalesForce] Update conversionRate in test class

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

Best Answer

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.

Related Topic