4

I am trying to test some code that uses UserInfo.getDefaultCurrency() but can't figure out how to control that value in a test.

For example this doesn't work:

    User u = new User(Id = UserInfo.getUserId(), LocaleSidKey = 'fr_FR');
    update u;
    System.runAs(u) {
        // Changes the thousand separator but not the UserInfo.getDefaultCurrency()
    }

Is there a way to change this in a test?

(In the Setup UI there is a "Currency Locale" field on the "Organization Detail".)

2

1 Answer 1

1

This question is a bit older, but I just ran into the same issue myself.

Maybe in June 2017 it was correct, what Keith C wrote, but today - API version 43 - the DefaultCurrencyIsoCode is writable in a multicurrency org.

However, in a single currency org the field does not exist, and currencies can only be changed org-wide.

@isTest
public class CurrencyTest {
    @isTest
    public static void testDefaultCurrenciesOnUsers() {
        Set<String> objectFields = Schema.SObjectType.User.fields.getMap().keySet();

        // This is for Multicurrency orgs
        if(objectFields.contains('DefaultCurrencyIsoCode')) {
            User u = new User(LastName = 'Tester1');
            u.put('DefaultCurrencyIsoCode', 'USD');
            insert u;
            System.runAs(u) {
                System.assertEquals('USD', UserInfo.getDefaultCurrency());
            }
            u = new User(LastName = 'Tester2');
            u.put('DefaultCurrencyIsoCode', 'EUR');
            insert u;
            System.runAs(u) {
                System.assertEquals('EUR', UserInfo.getDefaultCurrency());
            }
        }   
        else {
            System.assert(false, 'You are not in a multi-currency org.');
        }
    }
}
4
  • Good to know - thanks for posting including using dynamic field access.
    – Keith C
    Sep 19, 2018 at 11:36
  • You're welcome. With static access it just wouldn't compile in a single currency org ;-) Sep 19, 2018 at 14:50
  • Yep, makes sense but I didn't think of that...
    – Keith C
    Sep 19, 2018 at 14:55
  • And is there a way to change the org wide currency using Apex org any API?
    – Theodoor
    Nov 19, 2019 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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