[SalesForce] Apex native class to get the latest conversion rate from multiple currency

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

Best Answer

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.

Related Topic