Professional Services Automation Apex API Developer Reference

pse.MultiCurrencyService

global with sharing class MultiCurrencyService

This service provides the functionality to handle exchanges between multiple currencies in PSA.

Methods

useDatedExchangeRates

global static Boolean useDatedExchangeRates()

Indicates if dated exchange rates are used for multi-currency conversions when PSA is configured to use currencies specified in the Currencies tab.

Return Value

A Boolean indicating whether or not dated exchange rates are being used.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/* 
 * See if we are using dated exchanged rates in PSA
*/
System.debug(
    String.format(
        'Are we using dated exchange rates? {0}',
        new List<String>{
            (pse.MultiCurrencyService.useDatedExchangeRates()) ? 'Yes' : 'No'
        }
    )
);

useSalesforceAdvancedMultiCurrency

global static Boolean useSalesforceAdvancedMultiCurrency()

When set to true, PSA uses the Salesforce advanced multi-currency feature. When set to false, PSA uses the currencies defined in the Currencies tab.

Return Value

A Boolean indicating if PSA uses Salesforce Advanced Multi-Currency.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Display pertinent debugging messages based on number of PSA Currencies and
 * whether Salesforce Advanced Multi-Curency is configured
 */
List<appirio_core__Currency__c> psaCurrencies = [SELECT Name FROM appirio_core__Currency__c];
System.debug(String.format('You have ' + psaCurrencies.size() + ' PSA Currencies in your org.');
if(MultiCurrencyService.useSalesforceAdvancedMultiCurrency()){
    if(psaCurrencies.size() > 0){
        System.debug('However, you are using Salesforce Advanced Multi-Currency, so you don\'t really need them...');
    }else{
        System.debug('Don\'t worry, though... You\'re using Salesforce Advanced Multi-Currency!');
    }
}else{
    if(psaCurrencies.size() > 0){
        System.debug('Use them wisely!');
    }else{
        System.debug('You might want to create some... You\'re not using Salesforce Advanced Multi-Currency!');
    }
}

getConvertedAmount

global static Double getConvertedAmount(Double fromAmount, String fromCurrency, String toCurrency, Date d)

Convert amount A of currency X to currency Y as of date D.

Input Parameters

Name Type Description
fromAmount Double The Decimal amount in the currency from which the conversion is to be made.
fromCurrency String The String currency code from which the conversion is to be made.
toCurrency String The String currency code to which the conversion is to be made.
d Date The Date for which the currency exchange is to be made.

Return Value

A Double representing the resulting amount of the currency conversion.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Convert an amount from one currency to another
 * Foundational building block of multi-currency
 */
Decimal  fromAmount = 100.00,
         toAmount;
String fromCurrency = 'USD',
       toCurrency   = 'GBP';
Date conversionDate = Date.valueOf('2015-01-01');

toAmount = pse.MultiCurrencyService.getConvertedAmount(
    fromAmount,
    fromCurrency,
    toCurrency,
    conversionDate
);
System.debug(
    String.format(
        '{0} in {1} on {2} is equal to {3} in {4}.',
        new List<String>{
            fromAmount,
            fromCurrency,
            conversionDate,
            toAmount,
            toCurrency
        }
    )
);

getConversionRatesByDate

global static Map<Date, Map<String, Decimal>> getConversionRatesByDate(List<String> currencyCodeList, List<Date> dateList)

Use a list of currency codes and a list of dates to get a map of dated currency codes to conversion rates.

Input Parameters

Name Type Description
currencyCodeList List<String> A List of String currency codes for which to get Decimal conversion rates.
dateList List<Date> A List of Dates for which to get Decimal conversion rates.

Return Value

A Map of conversion Date to a Map of String currency codes to Decimal conversion rate.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Demonstrate that all four signatures of getConversionRatesByDate
 * return the same map of date to map of currency code to conversion rate
 */
Set<Date> setOfDates = new Set<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01')
};
List<Date> listOfDates = new List<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2005-01-01'),
    Date.valueOf('2015-01-01')
};
Set<String> setOfCurrencies = new Set<String>{'USD','GBP','CAD'};
List<String> listOfCurrencies = new List<String>{'USD','GBP','USD','CAD','GBP','CAD'};

Map<Date, Map<String, Decimal>> mapListAndList = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, listOfDates);
Map<Date, Map<String, Decimal>> mapSetAndList  = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  listOfDates);
Map<Date, Map<String, Decimal>> mapListAndSet  = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, setOfDates);
Map<Date, Map<String, Decimal>> mapSetAndSet   = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  setOfDates);

List<Map<Date, Map<String, Decimal>>> listOfMaps = new List<Map<Date, Map<String, Decimal>>>();
listOfMaps.add(mapListAndList);
listOfMaps.add(mapSetAndList);
listOfMaps.add(mapListAndSet);
listOfMaps.add(mapSetAndSet);
for(List<Map<Date, Map<String, Decimal>>> sourceMap : listOfMaps){
    for(List<Map<Date, Map<String, Decimal>>> compareMap : listOfMaps){
        System.assertEquals(compareMap, sourceMap, 'Maps should be equal.');
    }
}

getConversionRatesByDate

global static Map<Date, Map<String, Decimal>> getConversionRatesByDate(Set<String> currencyCodeSet, List<Date> dateList)

Use a set of currency codes and a list of dates to get a map of dated currency codes to conversion rates.

Input Parameters

Name Type Description
currencyCodeSet Set<String> A Set of String currency codes for which to get conversion rates.
dateList List<Date> A List of Dates for which to get conversion rates.

Return Value

A Map of conversion Date to a Map of String currency code to Decimal conversion rates.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Demonstrate that all four signatures of getConversionRatesByDate
 * return the same map of date to map of currency code to conversion rate
 */
Set<Date> setOfDates = new Set<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01')
};
List<Date> listOfDates = new List<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2005-01-01'),
    Date.valueOf('2015-01-01')
};
Set<String> setOfCurrencies = new Set<String>{'USD','GBP','CAD'};
List<String> listOfCurrencies = new List<String>{'USD','GBP','USD','CAD','GBP','CAD'};

Map<Date, Map<String, Decimal>> mapListAndList = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, listOfDates);
Map<Date, Map<String, Decimal>> mapSetAndList  = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  listOfDates);
Map<Date, Map<String, Decimal>> mapListAndSet  = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, setOfDates);
Map<Date, Map<String, Decimal>> mapSetAndSet   = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  setOfDates);

List<Map<Date, Map<String, Decimal>>> listOfMaps = new List<Map<Date, Map<String, Decimal>>>();
listOfMaps.add(mapListAndList);
listOfMaps.add(mapSetAndList);
listOfMaps.add(mapListAndSet);
listOfMaps.add(mapSetAndSet);
for(List<Map<Date, Map<String, Decimal>>> sourceMap : listOfMaps){
    for(List<Map<Date, Map<String, Decimal>>> compareMap : listOfMaps){
        System.assertEquals(compareMap, sourceMap, 'Maps should be equal.');
    }
}

getConversionRatesByDate

global static Map<Date, Map<String, Decimal>> getConversionRatesByDate(List<String> currencyCodeList, Set<Date> dateSet)

Use a list of currency codes and a set of dates to get a map of dated currency codes to conversion rates.

Input Parameters

Name Type Description
currencyCodeList List<String> A List of String currency codes for which to get conversion rates.
dateSet Set<Date> A Set of Dates for which to get conversion rates.

Return Value

A Map of conversion Date to a Map of String currency code to Decimal conversion rate.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Demonstrate that all four signatures of getConversionRatesByDate
 * return the same map of date to map of currency code to conversion rate
 */
Set<Date> setOfDates = new Set<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01')
};
List<Date> listOfDates = new List<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2005-01-01'),
    Date.valueOf('2015-01-01')
};
Set<String> setOfCurrencies = new Set<String>{'USD','GBP','CAD'};
List<String> listOfCurrencies = new List<String>{'USD','GBP','USD','CAD','GBP','CAD'};

Map<Date, Map<String, Decimal>> mapListAndList = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, listOfDates);
Map<Date, Map<String, Decimal>> mapSetAndList  = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  listOfDates);
Map<Date, Map<String, Decimal>> mapListAndSet  = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, setOfDates);
Map<Date, Map<String, Decimal>> mapSetAndSet   = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  setOfDates);

List<Map<Date, Map<String, Decimal>>> listOfMaps = new List<Map<Date, Map<String, Decimal>>>();
listOfMaps.add(mapListAndList);
listOfMaps.add(mapSetAndList);
listOfMaps.add(mapListAndSet);
listOfMaps.add(mapSetAndSet);
for(List<Map<Date, Map<String, Decimal>>> sourceMap : listOfMaps){
    for(List<Map<Date, Map<String, Decimal>>> compareMap : listOfMaps){
        System.assertEquals(compareMap, sourceMap, 'Maps should be equal.');
    }
}

getConversionRatesByDate

global static Map<Date, Map<String, Decimal>> getConversionRatesByDate(Set<String> currencyCodeSet, Set<Date> dateSet)

Uses a set of currency codes and a set of dates to get a map of dated currency codes to conversion rates.

Input Parameters

Name Type Description
currencyCodeSet Set<String> A Set of String currency codes for which to get conversion rates.
dateSet Set<Date> A Set of Dates for which to get conversion rates.

Return Value

A Map of conversion Date to a Map of String currency code to Decimal conversion rate.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Demonstrate that all four signatures of getConversionRatesByDate
 * return the same map of date to map of currency code to conversion rate
 */
Set<Date> setOfDates = new Set<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01')
};
List<Date> listOfDates = new List<Date>{
    Date.valueOf('2005-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2015-01-01'),
    Date.valueOf('2010-01-01'),
    Date.valueOf('2005-01-01'),
    Date.valueOf('2015-01-01')
};
Set<String> setOfCurrencies = new Set<String>{'USD','GBP','CAD'};
List<String> listOfCurrencies = new List<String>{'USD','GBP','USD','CAD','GBP','CAD'};

Map<Date, Map<String, Decimal>> mapListAndList = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, listOfDates);
Map<Date, Map<String, Decimal>> mapSetAndList  = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  listOfDates);
Map<Date, Map<String, Decimal>> mapListAndSet  = pse.MultiCurrencyService.getConversionRatesByDate(listOfCurrencies, setOfDates);
Map<Date, Map<String, Decimal>> mapSetAndSet   = pse.MultiCurrencyService.getConversionRatesByDate(setOfCurrencies,  setOfDates);

List<Map<Date, Map<String, Decimal>>> listOfMaps = new List<Map<Date, Map<String, Decimal>>>();
listOfMaps.add(mapListAndList);
listOfMaps.add(mapSetAndList);
listOfMaps.add(mapListAndSet);
listOfMaps.add(mapSetAndSet);
for(List<Map<Date, Map<String, Decimal>>> sourceMap : listOfMaps){
    for(List<Map<Date, Map<String, Decimal>>> compareMap : listOfMaps){
        System.assertEquals(compareMap, sourceMap, 'Maps should be equal.');
    }
}

getConversionRateRatioByDate

global static Decimal getConversionRateRatioByDate(String fromCurrencyCode, String toCurrencyCode, Date initialConversionDate, Date targetConversionDate)

Get a decimal conversion rate ratio to convert one converted amount of currency A on conversion date X to currency B on conversion date Y.

Input Parameters

Name Type Description
fromCurrencyCode String The String currency code from which the conversion is coming.
toCurrencyCode String The String currency code to which the conversion is targeting.
initialConversionDate Date The Date on which the exchange rate converted amount occurred.
targetConversionDate Date The Date for which you want the converted amount modified to reflect.

Return Value

A Decimal conversion rate ratio that can be used to modify a Decimal converted currency amount on on initial Date to another Date.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Get ratios to bring a couple previously converted amounts up to a newer date conversion amount
 */
Date Dec31_2014 = Date.valueOf('2014-12-31');
Date Jan1_2015  = Date.valueOf('2015-01-01');
Date Jan1_2016  = Date.valueOf('2016-01-01');

Map<Date, Map<String, Decimal>> mapDatesToMapCurrenciesToRates = appirio_core.MultiCurrencyUtil.getConversionRatesByDate(
    new Set<String>{
        'USD',
        'CAD'
    },
    new Set<Date>{
        Dec31_2014,
        Jan1_2015,
        Jan1_2016
    }
);

Map<Date, Decimal> X100USDonThisDateConvertedToCAD = new Map<Date, Decimal>{
    Dec31_2014=>(100*mapDatesToMapCurrenciesToRates.get(Dec31_2014).get('CAD')),
    Jan1_2015=>(100*mapDatesToMapCurrenciesToRates.get(Jan1_2015).get('CAD')),
    Jan1_2016=>(100*mapDatesToMapCurrenciesToRates.get(Jan1_2016).get('CAD'))
};

for(Date key : mapDatesToMapCurrenciesToRates.keySet()) {
    System.debug('100 USD on ' + String.valueOf(key) + ' was ' + X100USDonThisDateConvertedToCAD.get(key));
}

Decimal usd100toCADdec31_2014tojan1_2016ratio = 
    appirio_core.MultiCurrencyUtil.getConversionRateRatioByDate(
        'USD',
        'CAD',
        Dec31_2014,
        Jan1_2016
    );

System.assertEquals(
    X100USDonThisDateConvertedToCAD.get(Jan1_2016).setScale(2),
    (X100USDonThisDateConvertedToCAD.get(Dec31_2014) * usd100toCADdec31_2014tojan1_2016ratio).setScale(2)
);

Decimal crrCADvarDec31_2014effectiveUSDfixedJan1_2015current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'CAD', // currencyCodeVariable
        'USD', // currencyCodeFixed
        Dec31_2014, // effectiveDate
        Jan1_2015 // currentDate
);
Decimal crrCADvarDec31_2014effectiveUSDfixedJan1_2016current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'CAD', // currencyCodeVariable
        'USD', // currencyCodeFixed
        Dec31_2014, // effectiveDate
        Jan1_2016 // currentDate
);
Decimal crrCADvarJan1_2015effectiveUSDfixedJan1_2016current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'CAD', // currencyCodeVariable
        'USD', // currencyCodeFixed
        Jan1_2015, // effectiveDate
        Jan1_2016 // currentDate
);
Decimal crrUSDvarDec31_2014effectiveCADfixedJan1_2015current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'USD', // currencyCodeVariable
        'CAD', // currencyCodeFixed
        Dec31_2014, // effectiveDate
        Jan1_2015 // currentDate
);
Decimal crrUSDvarDec31_2014effectiveCADfixedJan1_2016current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'USD', // currencyCodeVariable
        'CAD', // currencyCodeFixed
        Dec31_2014, // effectiveDate
        Jan1_2016 // currentDate
);
Decimal crrUSDvarJan1_2015effectiveCADfixedJan1_2016current = 
    pse.MultiCurrencyService.getConversionRateRatioByDate(
        'USD', // currencyCodeVariable
        'CAD', // currencyCodeFixed
        Jan1_2015, // effectiveDate
        Jan1_2016 // currentDate
);
System.debug('crrCADvarDec31_2014effectiveUSDfixedJan1_2015current: ' + crrCADvarDec31_2014effectiveUSDfixedJan1_2015current);
System.debug('crrCADvarDec31_2014effectiveUSDfixedJan1_2016current: ' + crrCADvarDec31_2014effectiveUSDfixedJan1_2016current);
System.debug('crrCADvarJan1_2015effectiveUSDfixedJan1_2016current: '  + crrCADvarJan1_2015effectiveUSDfixedJan1_2016current);
System.debug('crrUSDvarDec31_2014effectiveCADfixedJan1_2015current: ' + crrUSDvarDec31_2014effectiveCADfixedJan1_2015current);
System.debug('crrUSDvarDec31_2014effectiveCADfixedJan1_2016current: ' + crrUSDvarDec31_2014effectiveCADfixedJan1_2016current);
System.debug('crrUSDvarJan1_2015effectiveCADfixedJan1_2016current: '  + crrUSDvarJan1_2015effectiveCADfixedJan1_2016current);

isCurrencyCorporate

global static Map<String, Boolean> isCurrencyCorporate(Set<String> currencyCodeSet)

Uses a set of currency codes to return a map of currency codes and associated Booleans indicating if the currency code is corporate.

Input Parameters

Name Type Description
currencyCodeSet Set<String> A Set of Strings representing the currencies to check.

Return Value

A Map of String currency codes to Boolean indicating whether the currency is the corporate one.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Check to see if each currency in a set is the corporate one
 */
Set<String> setCurrencyCodes = new Set<String>{
    'USD',
    'CAD',
    'GBP'
};
Date d = Date.today();
Map<String, Boolean> mapCodesToCorporateBoolean = pse.MultiCurrencyService.isCurrencyCorporate(setCurrencyCodes);
for(String codeKey : mapCodesToCorporateBoolean.keySet()){
    System.debug(
        String.format(
            'Currency {0} is {1} the corporate currency.',
            new List<String>{
                codeKey,
                (mapCodesToCorporateBoolean.get(codeKey)) ? '' : 'not'
            }
        )
    );
}

pse.MultiCurrencyService.ExchangeRate

global with sharing class ExchangeRate

ExchangeRate provides a Multi-Currency Helper Class

Properties

Name Type Description
currentScale Integer The scale of precision to use when converting currencies. See System.Decimal.divide(). @return An Integer representing the number of decimal places of precision.

Methods

ExchangeRate

global ExchangeRate()

Constructor. Creates an empty ExchangeRate.

Return Value

An empty ExchangeRate.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Create blank wrapper
 */
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper();
System.debug(erw);

ExchangeRate

global ExchangeRate(Set<String> currencyCodeSet, Set<Date> dateSet)

Constructor. Using a set of currency codes and a set of dates, creates an ExchangeRate for all combinations of these currencies and dates.

Input Parameters

Name Type Description
currencyCodeSet Set<String> A Set of String currency codes for which to load rates.
dateSet Set<Date> A Set of Dates for which to load the given currency code rates.

Return Value

An ExchangeRate with permutations for all currencies and dates provided.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Load an exchange rate wrapper with a set of dates and currencies
 */
Set<String> currencyCodes = new Set<String>{
    'USD',
    'CAD',
    'GBP'
}
Set<Date> dateSet = new Set<Date>{
    Date.valueOf('2015-01-01'),
    Date.valueOf('2016-01-01')
};
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper(currencyCodes, dateSet);
for(String currencyCode : currencyCodes){
    for(Date d : dateSet){
        System.debug(
            String.format(
                'Rate for {0} on {1} is {2}.',
                new List<String>{
                    currencyCode,
                    String.valueOf(d),
                    String.valueOf(erw.getDatedRate())
                }
            )
        );
    }
}

ExchangeRate

global ExchangeRate(Set<String> currencyCodeSet, Set<Date> dateSet, Integer scale)

Constructor. Using a set of currency codes and a set of dates, creates an ExchangeRate for all combinations of these currencies and dates for the given precision scale.

Input Parameters

Name Type Description
currencyCodeSet Set<String> A Set of String currency codes for which to load rates.
dateSet Set<Date> A Set of Dates for which to load the given currency code rates.
scale Integer An Integer number representing the number of decimal places of precision.

Return Value

An ExchangeRate with permutations for all currencies and dates provided of given scale precision.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Load an exchange rate wrapper with a set of dates and currencies of given precision scale
 */
Set<String> currencyCodes = new Set<String>{
    'USD',
    'CAD',
    'GBP'
}
Set<Date> dateSet = new Set<Date>{
    Date.valueOf('2015-01-01'),
    Date.valueOf('2016-01-01')
};
Integer scale = 2;
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper(currencyCodes, dateSet, scale);
for(String currencyCode : currencyCodes){
    for(Date d : dateSet){
        System.debug(
            String.format(
                'Rate for {0} on {1} is {2}.',
                new List<String>{
                    currencyCode,
                    String.valueOf(d),
                    String.valueOf(erw.getDatedRate())
                }
            )
        );
    }
}

addDatedCurrency

global void addDatedCurrency(String currencyCode, Date conversionDate)

Adds a dated currency for given currency on given date.

Input Parameters

Name Type Description
currencyCode String A String indicating a currency code to add.
conversionDate Date A Date on which to add the given currency code.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Try pulling dated rate before adding, then pull after adding
 */
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper();
String currencyCode = 'USD';
Date rateDate = Date.today();
try{
    erw.getDatedRate(currencyCode, rateDate);
    System.assert(false, 'Should not have rate for given currency and date yet.')
}catch(Exception e){
    System.debug(
        String.format(
            'No rate found for {0} on {1}.',
            new List<String>{
                currencyCode,
                String.valueOf(rateDate)
            }
        )
    );
}
erw.addDatedCurrency(currencyCode, rateDate);
try{
    System.debug(
        String.format(
            'Conversion rate for {0} on {1} is {2}.',
            new List<String>{
                currencyCode,
                String.valueOf(rateDate),
                String.valueOf(erw.getDatedRate(currencyCode, rateDate))
            }
        )
    );
}catch(Exception e){
    System.assert(
        false,
        String.format(
            'Should have rate for currency {0} and date {1}.',
            new List<String>{
                currencyCode,
                String.valueOf(rateDate)
            }
        )
    );
}

getDatedRate

global Decimal getDatedRate(String currencyCode, Date rateDate)

Get the exchange rate for a given currency on a given date.

Input Parameters

Name Type Description
currencyCode String A String representing the currency code for the exchange rate desired.
rateDate Date A Date on which the exchange rate for given currency is desired.

Return Value

A Decimal representing the exchange rate for given currency on given date.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Pull dated rate after adding
 */
String currencyCode = 'USD';
Date rateDate = Date.today();
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper(
    new Set<String>{currencyCode},
    new Set<Date>{rateDate}
);
System.debug(
    String.format(
        'Conversion rate for {0} on {1} is {2}.',
        new List<String>{
            currencyCode,
            String.valueOf(rateDate),
            String.valueOf(erw.getDatedRate(currencyCode, rateDate))
        }
    )
);

convertAmount

global Decimal convertAmount(Decimal fromAmount, String fromCurrencyCode, Date fromDate, String toCurrencyCode, Date toDate)

Convert amount A1 from currency C1 on date D1 to currency C2 on date D2.

Input Parameters

Name Type Description
fromAmount Decimal A decimal representing the amount in the source currency.
fromCurrencyCode String A String representing the currency code of the source currency.
fromDate Date The Date on which the fromCurrency is being considered for the conversion.
toCurrencyCode String A String representing the currency code of the destination currency.
toDate Date The Date on which the toCurrency is being considered for the conversion.

Return Value

A Decimal representing the converted amount in the given toCurrencyCode.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

/**
 * Convert an amount using an exchange rate wrapper with a set of dates and currencies
 */
String usDollars       = 'USD',
       canadianDollars = 'CAD',
       poundsSterling  = 'GBP';
Set<String> currencyCodes = new Set<String>{
    usDollars,
    canadianDollars,
    poundsSterling
}
Date jan1_2015 = Date.valueOf('2015-01-01'),
     jan1_2016 = Date.valueOf('2016-01-01');
Set<Date> dateSet = new Set<Date>{
    jan1_2015,
    jan1_2016
};
pse.MultiCurrencyService.ExchangeRateWrapper erw = new pse.MultiCurrencyService.ExchangeRateWrapper(currencyCodes, dateSet);
Decimal amountToConvert = 100.00;
Decimal x100USDinGBP = erw.convertAmount(
    amountToConvert,
    usDollars,
    jan1_2015,
    poundsSterling,
    jan1_2015
);
System.debug(
    String.format(
        '{0} in {1} on {2} was {3}.',
        new List<String>{
            String.valueOf(amountToConvert),
            usDollars,
            String.valueOf(jan1_2015),
            String.valueOf(x100USDinGBP)
        }
    )
);
© Copyright 2009–2023 Certinia Inc. All rights reserved. Various trademarks held by their respective owners.