3

I am trying to do something like this:

List<object__c> testObject= [
    SELECT SUM(convertCurrency(Amount__c))
    FROM object__c
];

but it doesn't work. As far as I read in the Force.com SOQL and SOSL Reference it says that this is not possible to be achieved. From Querying Currency Fields in Multi-currency Orgs:

You can’t convert the result of an aggregate function into the user’s currency by calling the convertCurrency() function. If a query includes a GROUP BY or HAVING clause, currency data returned by using an aggregate function, such as SUM() or MAX(), is in the org’s default currency.

However I am trying to avoid having to iterate over a giant list of objects just to sum up their converted currencies. Is there an easy way to achieve what I am trying to do within the SOQL?

2
  • 2
    I do believe that you have to iterate post-query. Then again, last time I had to try was almost four years ago.
    – Adrian Larson
    Commented Sep 9, 2016 at 21:33
  • Thanks for your response and editing my question adding a link to it!
    – Arthlete
    Commented Sep 9, 2016 at 21:33

1 Answer 1

5

Last time I checked, you still have to iterate the results post-query. The documentation you referenced does indeed state as much in pretty clear terms. Not elegant, but it's a complex feature.

Something like:

Decimal sum = 0;
for (MyObject__c record : [SELECT convertCurrency(Amount__c) amount FROM MyObject__c])
    sum += (Decimal)record.get('amount');
0

You must log in to answer this question.

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