Find your content:

Search form

You are here

CurrencyIsoCode - Auto included in query results, except for test cases

 
Share

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.


Attribution to: James Davies

Possible Suggestion/Solution #1

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!


Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4182

My Block Status

My Block Content