Automate Exchange Rates with HTTP Callout
Update: Thanks to a great tip from Flowfest winner Yumi Ibrahimzade, this post has been updated to make it even simpler and I managed to eliminate a couple of steps. Thanks Yumi!
Update (2 Feb): I just fixed an error where the URL path was not passed when creating the HTTP Callout – thanks to Matt Suellentrop for helping to find my mistake! (See the correction in step 5).
Update (18 Feb) – Håkon Dieset helped find another mistake, I had one of the parameters named base instead of source. This caused all conversions to be done using USD as the main currency. Corrected now in Step 5.
If your Salesforce environment is using multiple currencies, one of the painful admin tasks is to upload the new exchange rates every day/week/month. If you have many currencies to manage this gets even more painful.
You can now (in Spring ’23) say goodbye to manually entering these dated exchange rates!
Using the new HTTP Callout feature (Spring ’23) in flow to look up the current rates on the Internet and automatically create new dated exchange rate records in Salesforce.
Currency Data API provides exchange rates for 168 currencies.
Spot exchange rate data is retrieved from several major forex data providers in real-time, validated, processed and delivered hourly, every 10 minutes, or even within the 60-second market window.
Providing the most representative forex market value available (“midpoint” value) for every API request, the Currency Data API API powers currency converters, mobile applications, financial software components and back-office systems all around the world.
Using a FREE account from the Currency Data API gives you 100 transactions per month – more than enough to schedule this to run once every day. Simply set up the flow to run overnight and the next day your rates will be loaded and ready to go!
Before you start
Before you can Automate Exchange Rates with HTTP Callout, you need to have multi-currency enabled in your Salesforce org before you start or you will not have the object: Dated Conversion Rate which is used in the flow.
As the HTTP Callout is a beta feature only available in Spring ’23 organizations you need a sandbox on this release level to implement this.
Automate Exchange Rates with HTTP Callout
Step 1: Get your free account at Currency Data API
A free account from APILayer will give you 100 requests per month. More than enough to do a daily update of your exchange rates. One request can return multiple rates so you should only use 28-31 requests per month, regardless of how many currencies you need exchange rates for.
Go here to get your FREE account:
Once you have subscribed, you will receive an API Key – copy this to your clipboard for the next step.
Step 2: Create a Custom Label to store your new API Key
The API Key is like a password to use the API service. Store it in a Custom Label so you can easily access it via your flow using the global variable $Label.
Paste the API Key into the value field shown below:
Salesforce -> Setup -> Custom Labels -> New
Step 3: Add a Named Credential for the API
The Named Credential will be used as the destination for the request (HTTP Callout) from Salesforce.
For this API, we will be using: https://api.apilayer.com/currency_data/
Salesforce -> Setup -> Named Credentials -> New Legacy
*** Make sure you select “New Legacy” not “New” when creating this ***
Give the new credential a label, click the tab key to automatically create the name and then enter the URL.
Tick the remaining three checkboxes and then save.
Step 4: Create a Scheduled Flow to perform the update
Problem: When a scheduled flow runs, Salesforce opens a DML transaction. Even though you do not specify an object in the start criteria, it still opens a transaction. An HTTP Callout cannot be performed when there is an open DML transaction. (I learned this the hard way).
Solution: Thanks to a tip from Yumi Ibrahimzade, you can just add a pause element before the HTTP Callout and this problem will be solved.
Create a new flow and select Schedule-Triggered Flow.
Set a schedule for when to run the flow:
Add a Pause element to the flow to solve the HTTP Callout problem described above.
On the second tab of the pause element (Resume Event), set the resume time:
Add an action and select Create HTTP Callout (Beta)
Step 5: Create the new HTTP Callout action
Configure the new HTTP Callout action as follows:
The Named Credential uses the one you configured earlier.
Add a Label (1)
Select the GET method (2)
Enter a description (3)
Add the URL Path (4) to use the live rates function from this API service
Click Add Key (5)
Add these three keys (all of the type string):
Note the correction to the name of the 2nd key (18 Feb).
Before clicking Save, you need to provide a sample JSON response.
Go back to the API Layer website in a new browser tab:
(don’t close the Salesforce tab – you will come back to it)
Make sure you are still logged in to this website. (or the run code button will not work).
Click the Live Demo button:
Select the GET /live option (1) on the left (you may need to collapse the currently open option)
Enter the currencies you want to be converted to (2) – these equal the “symbols” parameter you configured earlier. If you need multiple currencies, separate them with commas.
Enter the source (3) – this equals the “source” currency you configured earlier.
Click the Run Code button (4)
Copy the text that is returned including the braces (5)
Return to Salesforce to finish configuring the HTTP Callout:
Click New to create a sample response:
Paste in the response (on the left panel) and click Review to verify it is correct:
If you get a Data Structure like shown on the right (above) then all is good and you can click done.
In the background, Salesforce has done all the hard work for you in creating an external service and the Apex data type to handle the response plus making it available as an invocable action for use by the flow.
Step 6: Configure your flow to use the new HTTP Callout
Now your new action has been created (and is available for other flows too) it’s time to configure it for use in this flow: You need to pass it the apikey, source currency, and symbols (rates you are converting to).
Add a Label (1)
Select the API key you created a label for back at the start (via the global $Label variable) (2)
Enter your source currency (3)
Enter a comma-delimited list of currencies you want to convert to (4)
Step 7: Create formula fields to store the results from the HTTP Callout
The results coming back from the HTTP callout don’t seem to play nicely with flow so we need to assign them to a formula field so we can use them in flow.
Create a new formula field for each returned currency value:
Note the use of 6 decimal places.
Drill into the results of our action to get the returned values:
Select > beside the Outputs from your Get_FX_Rates action:
Choose > beside the 2XX value:
Select > beside quotes:
Select the value you want to be assigned to the formula:
Repeat this process for all other currencies (eg the GBP currency):
The completed EURRate formula:
The completed GBPRate formula:
Step 8: Create the new Dated Exchange Records
Add a Create Records element for each currency you need and populate the fields as shown.
Create the EUR record:
Add another Create Records for the GBP record:
Your final flow should look like this:
Save and activate your flow.
Double-check your flow is now active.
When the time schedule passes you should have new entries in your Managed Currencies!
(If not, check your email for an error!)
Setup -> Manage Currencies -> Manage Dated Exchange Rates
Running the flow multiple times per day
If you have a business requirement to run this flow multiple times per day to update your rates then this is possible but adds some complexity.
- Salesforce only allows one Dated Exchange Rate to be added per day so you will need to adjust the flow to check if the record already exists for that currency for today and to update it rather than creating a new record. Do a Get Records and if it returns a result, edit the record, otherwise create a new one.
- You will likely run out of API calls (100 on the free subscription) – you could probably do 3 per day but beyond that, you will need a paid subscription.
- You will need to find a new way to run the scheduled task as Scheduled Flows can only be set to run on a daily/weekly schedule. (you could just duplicate the flow and have several copies running at different times).
Need Flow Training?
Like what you see but need some training on flow? My Learning Salesforce Flow course is designed for beginners to get started with building flows. I will take you step-by-step through building 25 flows that cover all of the key elements available. You get 24 x 7 access to the course with a lifetime subscription.