Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a managed travel expense app with an AggregateResult SOQL that sums up Expenses per Day. All expenses can have different currencies and don't have to match the corporate nor the current users currency.

public with sharing class CtrlExt {
   ...

   Map<Date, Decimal> expensesPerDay = new Map<Date, Decimal>();

   for(AggregateResult ar : [SELECT dat_DatField__c day, SUM(cur_Amount__c) sum FROM   Expense__c GROUP BY dat_DatField__c]) {
      Date day = (Date) ar.get('day');
      Decimal daySum = (Decimal) ar.get('sum');

      expensesPerDay.put(day, daySum);
   }
   ...
}

My questions are:

  1. Does daySum take all the different currencies into account?
  2. The resulting Decimal daySum implicitly uses which Currency? Org, User?
  3. If I write the value of daySum into a Currency field would the Currency Iso Code be automaticall set correctly ? Correctly means that if I display it with on a VF page would the Currency and the value be what the user expects?
share|improve this question

1 Answer 1

up vote 11 down vote accepted

Source: http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_querying_currency_fields.htm

1) Sum() calculates different currencies correctly.

2) The result of Sum() uses the Corporate Currency of your org.

3) No, you have to set the CurrencyIsoCode to your Corporate Currency CurrencyIsoCode manually.

You can test that by adding a debug statement to your code and run this with different Corporate Currencies:

public with sharing class CtrlExt {
   ...

   Map<Date, Decimal> expensesPerDay = new Map<Date, Decimal>();

   for(AggregateResult ar : [SELECT dat_DatField__c day, SUM(cur_Amount__c) sum FROM   Expense__c GROUP BY dat_DatField__c]) {
      Date day = (Date) ar.get('day');
      Decimal daySum = (Decimal) ar.get('sum');

      System.debug('Day: ' + day + ' / Sum: ' + sum);

      expensesPerDay.put(day, daySum);
   }
   ...
}

I tested it with Euro and with US Dollar as Corporate Currency, in my case the log gives me the following values:

Corporate Currency USD: DEBUG|Day: 2014-06-09 00:00:00 / Sum: 755.74
Corporate Currency EUR: DEBUG|Day: 2014-06-09 00:00:00 / Sum: 546.25

share|improve this answer
1  
It would be great to have some official information linked to your nice answer. Thanks. –  mast0r Jun 27 at 12:45
1  
@mast0r Sorry, added a link to the documentation. –  David Renz Jun 27 at 12:54
    
Many thanks, looks good! :) –  mast0r Jun 27 at 12:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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