How to import Dated Exchange Rates to Salesforce

A few months ago during reviewing the Sales Cloud certification exam I had a debate with a few people from different sides of the world about multi currencies in Salesforce. Topic, which should bother mainly us in Czech Republic as we don’t have Euro but our great Czech Crowns.

The surprising outcome (for them) was, that on most instances I ever saw, the customers enabled multi currency but not advanced multi currency, which enables dated exchange rates.

How they handle situation when they need to change the exchange rate and it will change everything, they asked?

They don’t was my answer, they are more scared about the fact that some roll-ups will stop working and that they will need to manually enter the exchange rates on daily/weekly/monthly or what ever bases they decide.

The great thing about dated exchange rates, besides the fact it is supported only on a few objects and most roll-ups will stop working, is, that you cannot create records in this object with simple DML operation but you need to call Salesforce REST API.

Anyway, after a few weeks customers asked exactly for this and I had a fun for a while.

It was pretty easy to get the exchange rates from our national bank, they publish them in simple but easy to parse text format, but how to call the Salesforce REST API was a bit more tricky as I didn’t want to hard code (nor even store in CMDT) my login details. Perfect time to play with the Named Credentials.

It is super simple to create a named credentials, but if you want to use oAuth flow you need to do a bit more.

  1. create a connected app, allow Full Access and Refresh Token scopes, set callback URLs to anything valid (they say, at the end I finished with a few to be sure such as https://login.salesforce.com and https://mydomain.my.salesforce.com/anyexistingpage)
  2. wait a few minutes (after each change of connected app, so you are never really sure what worked)
  3. set-up and Auth. Provider of type Salesforce with your own Client Id and Client Secret from the connected app, I added full refresh_token to the scope field
  4. create a named credentials and choose the Auth. Provider. If all goes to plan you should that you were authenticated, if it says „without refresh token“ you did a mistake during configuration of Auth. Provider or Connected App

The APEX code was super simple and can definitely be written better:

req2.setEndpoint('callout:ourSF/services/data/v51.0/sobjects/DatedConversionRate/'); 
req2.setHeader('Content-Type', 'application/json');
req2.setMethod('POST');
req2.setBody('{ "IsoCode" : "CZK", "ConversionRate" : ' + exchangeRate + ', "StartDate" : "' + fromDate.year() + '-' + fromDate.month() + '-' + fromDate.day() + '" }');

But somehow it didn’t really work. But than I added one more header and surely (as they said on StackOverflow) magic happened.

req2.setHeader('Authorization','Bearer {!$Credential.OAuthToken}'); 

So now we have actual exchange rate as often we want without any manual work from the admin. Win!

Leave a Reply