9

I've implemented some functionality around MultiCurrency in a Managed package. Since it needs to be able to support non-multi-currency orgs, all references to the CurrencyIsoCode field are done using Dynamic apex.

When requesting a record via SOQL, it appears that the CurrencyIsoCode field is transparently added to the resultset when executing code in the UI - I discovered this by accident and was pleasantly surprised that it works.

For example, the following query

Order__c order = [SELECT Name FROM Order__c WHERE Id = :op_id];
String currencyCode = order.get('CurrencyIsoCode');

Will successfully fetch the Currency Iso Code when executed normally via the user, even through I didn't explicitly include that field in the SOQL query.

However, the same code fails in a Test Case with the expected error "SObject row was retrieved via SOQL without querying the requested field: CurrencyIsoCode"

Am I missing something? How come this works in Production but not in actual test code? It would be nice to be able to keep the above code rather than modifying it to use (somewhat messier) dynamic SOQL.

3
  • 1
    Just wondering if adding @seeAllData changes anything, not that I'd recommend using it. Also you're not setting a value in your code, coz this strange behaviour is observed salesforce.stackexchange.com/questions/3648/… Nov 8, 2012 at 7:41
  • SeeAllData makes no difference; Also tried with 'testmethod' keyword. Nov 8, 2012 at 7:49
  • Yep same here, lol, most odd. Nov 8, 2012 at 7:50

1 Answer 1

4

Wow that is strange and can confirm I have just tried it myself! Also agree would like to see that become the default! It is not great to have to resort to Dynamic SOQL just for this and loose the compile time checking and dependency enforcement it brings, especially when packaging solutions on the platform.

We have partly worked around this and the dynamic introduction of CurrencyIsoCode in this Selector Enterprise App pattern, while also leveraging Apex Describe literals to retain the compile time checking and dependency references.

In the meantime I would really like to know from Salesforce if this was an accident or if the lack of this in test is a bug that will someday be fixed!

You must log in to answer this question.

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