0

We have an org which is using multiple currency. I have the DatedConversionobject which has all the currencies stored with different currency rates for different date criterias. I would like to know if there is an inbuilt apex class or method which would get the latest currency conversion rate based on the iso code and date? How do you guys handle this scenario.. Buyan

2
  • What's the purpose of doing the conversion? Usually, the platform takes care of it for you, so I'd advise against trying to perform manual calculations unless it's actually appropriate.
    – sfdcfox
    Dec 14, 2016 at 20:05
  • @sfdcfox I agree. But the requirement is that they have some custom amount fields where they want to see the converted amount in USD. There is a custom object called service linked to the opportunity where if the user enters say 100 Yuan based on his currency, a custom field called expected revenue should be populated in USD based on the conversion rate. So to compute this, a trigger has been written which queries the entire Datedcurrencyobject and puts it in a map causing an apex heap limit. So i want to see if there is a native class which can do that for me instead of querying.. Dec 14, 2016 at 20:35

1 Answer 1

0

I don't think there is a built in system function to get this. However, I haven't tested it, but I believe you could do something like this:

public class DatedConversionRateRetriever{
  private static Map<String, DatedConversionRate> conversionRates = new Map<String, DatedConversionRate>();

  public static DatedConversionRate getConversionRate(String isoCode){
    if(!conversionRates.containsKey(isoCode)){
        DatedConversionRate rate = [SELECT Id, isoCode, Conversionrate, nextStartDate, startDate 
          FROM DatedConversionRate 
          WHERE isoCode = :isoCode 
          ORDER BY NextStartDate DESC Limit 1][0]
        conversionRates.put(isoCode, rate);
     }

    return conversionRates.get(isoCode);
  }
}

This would return future conversion rates if they exist (idk if that's how multi-currency actually works?). If you want the active one, add WHERE StartDate <= TODAY AND NextStartDate > TODAY.

2
  • I think your function will work and solve our need for now. I hate to do this query each time on an insert but untill salesforce finds a way to cash this object in memory, we do not have an alternative.. Dec 14, 2016 at 22:18
  • @buyankumar, if you are going to be potentially calling this multiple times, you should cache the results statically. I've updated the answer to show what that would look like.
    – NSjonas
    Dec 15, 2016 at 1:56

You must log in to answer this question.

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