function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Query fields dependent on UserInfo variable.

How to query CurrencyISOCode in SOQL when organization is Multi Currency (UserInfo.isMultiCurrencyOrganization) and don't query that otherwise, and in the same query filter by an array of ids (...where id in :ids )?
Why not keep it simple and have an if/else statement?


if(UserInfo.isMultiCurrencyOrganization == true){
   //soql statement that includes the currencyIsoCode field in the where clause
  // soql statement that does not include the currencyIsoCode field in the where clause

This solution doesn't do it in one single line of code, but it still will only execute 1 SOQL statement in either case.

I hope this helps.
I assume that single currency organization doesn't has this CurrencyISOCode field so the code won't compile there.
Yes, great point.

Lets go with this approach.
1. Do a Describe on the object for which you want to query
2. Check the Describe Results to see if there is a field 'currencyIsoCode'
3. If yes, add it to the where clause as needed.
4. Use Dynamic SOQL to query.

Here is a link to more information on Dynamic Apex :

It involves a little more code than our previous solution (which is invalid regardless), but this should resolve your requirement and support if multi-currency is enabled or not.

Yes. In fact I have finally done something quite similar to what you suggest

When I loop through rows selected without the currency code ( array of ids is in the where clause ) i call database.query where i search for a record with the particular actual being looped id, and then i take element [0] and using sObject.get i take the value.
Nasty and maybe vurnalable code but what to do... ;)