Use Case :
The main objective of reading this blog is to learn how daily scheduled Flow updates live currency exchange rates to Salesforce Dated Exchange Rates. You must enable multi currency and sophisticated currency management in the desired org before you start. Otherwise, the Dated Exchange Rate Object utilised in this flow won’t be available to you. To implement this, you must use a sandbox with the Spring 23 release level or higher.
Steps :
To create a Custom Label. To create a HTTP Callout Apex Class. To get a live Currency API key. To create a Scheduled Flow.
Step 1 :
Obtaining an API key is the first step in using the Currency Data API, which provides real-time exchange rates. Exchange rates for 168 nations are provided through the Currency Data API. Although there are several API marketplaces, I’m acquiring my API key through the “API Layer” (https://apilayer.com/) marketplace in this case. For the API Layer’s Currency Data API, a free membership with 100 callouts is offered.
Step 2 :
Create a Custom Label with desired name and save the copied API Key in the label.
Step 3 :
- Create an apex class with Invocable and future methods.
- In this below class, Future method “getExchangeRates” will make an api call to the external system using the api key i.e. the Custom Label we created with the api key in the previous step. In the next step, Invocable method insertExchangeRates will be invoked in the scheduled flow.
Apex Class :
/**
* Description : Callout class insert Dated Exchange Rates
* Author : Vignesh
* Date : 27/03/2023
*/
public without sharing class CallOutClass {
public static final String API_KEY = Label.Currency_API;
/**
* Descrition : Method to insert Dated Exchange Rates
*/
public static void processRates(Decimal GBP){
Http httpItem = new Http();
HttpRequest req = new HttpRequest();
String bodyText = '{';
bodyText += '"ISOCode" : "GBP",';
bodyText += '"ConversionRate" :'+GBP+',';
bodyText += '"StartDate" : "'+String.ValueOf(Date.today())+'"' ;
bodyText += '}';
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() +
'/services/data/v57.0/sobjects/DatedConversionRate/');
req.setBody(bodyText);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
HttpResponse res = httpItem.send(req);
}
/**
* Descrition : Method to get live exchange rates by sending http request.
*/
@future(callout = true)
public static void getExchangeRates() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.apilayer.com/currency_data/live?base=USD&symbols=EUR,GBP
');
req.setMethod('GET');
req.setHeader('apikey', API_KEY);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Inherit auth from parent');
Http http = new Http();
HttpResponse res = http.send(req);
reponseWrapper wrapperItem = new reponseWrapper();
wrapperItem = parse(res.getBody());
System.debug('wrapperItem'+wrapperItem);
if(wrapperItem != null){
processRates(wrapperItem.quotes.USDGBP);
}
}
@InvocableMethod(callout=true label='Insert Exchange Rates')
public static void insertExchangeRates(){
getExchangeRates();
}
public class reponseWrapper {
public Boolean success;
public Integer timestamp;
public String source;
public Quotes quotes;
}
public class Quotes {
public Double USDAED;
public Double USDAFN;
public Double USDALL;
public Double USDAMD;
public Double USDANG;
public Double USDAOA;
public Double USDARS;
public Double USDAUD;
public Double USDAWG;
public Double USDAZN;
public Double USDBAM;
public Double USDBBD;
public Double USDBDT;
public Double USDBGN;
public Double USDBHD;
public Double USDBIF;
public Double USDBMD;
public Double USDBND;
public Double USDBOB;
public Double USDBRL;
public Double USDBSD;
public Double USDBTC;
public Double USDBTN;
public Double USDBWP;
public Double USDBYN;
public Double USDBYR;
public Double USDBZD;
public Double USDCAD;
public Double USDCDF;
public Double USDCHF;
public Double USDCLF;
public Double USDCLP;
public Double USDCNY;
public Double USDCOP;
public Double USDCRC;
public Double USDCUC;
public Double USDCUP;
public Double USDCVE;
public Double USDCZK;
public Double USDDJF;
public Double USDDKK;
public Double USDDOP;
public Double USDDZD;
public Double USDEGP;
public Double USDERN;
public Double USDETB;
public Double USDEUR;
public Double USDFJD;
public Double USDFKP;
public Double USDGBP;
public Double USDGEL;
public Double USDGGP;
public Double USDGHS;
public Double USDGIP;
public Double USDGMD;
public Double USDGNF;
public Double USDGTQ;
public Double USDGYD;
public Double USDHKD;
public Double USDHNL;
public Double USDHRK;
public Double USDHTG;
public Double USDHUF;
public Double USDIDR;
public Double USDILS;
public Double USDIMP;
public Double USDINR;
public Double USDIQD;
public Double USDIRR;
public Double USDISK;
public Double USDJEP;
public Double USDJMD;
public Double USDJOD;
public Double USDJPY;
public Double USDKES;
public Double USDKGS;
public Double USDKHR;
public Double USDKMF;
public Double USDKPW;
public Double USDKRW;
public Double USDKWD;
public Double USDKYD;
public Double USDKZT;
public Double USDLAK;
public Double USDLBP;
public Double USDLKR;
public Double USDLRD;
public Double USDLSL;
public Double USDLTL;
public Double USDLVL;
public Double USDLYD;
public Double USDMAD;
public Double USDMDL;
public Double USDMGA;
public Double USDMKD;
public Double USDMMK;
public Double USDMNT;
public Double USDMOP;
public Double USDMRO;
public Double USDMUR;
public Double USDMVR;
public Double USDMWK;
public Double USDMXN;
public Double USDMYR;
public Double USDMZN;
public Double USDNAD;
public Double USDNGN;
public Double USDNIO;
public Double USDNOK;
public Double USDNPR;
public Double USDNZD;
public Double USDOMR;
public Double USDPAB;
public Double USDPEN;
public Double USDPGK;
public Double USDPHP;
public Double USDPKR;
public Double USDPLN;
public Double USDPYG;
public Double USDQAR;
public Double USDRON;
public Double USDRSD;
public Double USDRUB;
public Double USDRWF;
public Double USDSAR;
public Double USDSBD;
public Double USDSCR;
public Double USDSDG;
public Double USDSEK;
public Double USDSGD;
public Double USDSHP;
public Double USDSLE;
public Double USDSLL;
public Double USDSOS;
public Double USDSRD;
public Double USDSTD;
public Double USDSVC;
public Double USDSYP;
public Double USDSZL;
public Double USDTHB;
public Double USDTJS;
public Double USDTMT;
public Double USDTND;
public Double USDTOP;
public Double USDTRY;
public Double USDTTD;
public Double USDTWD;
public Double USDTZS;
public Double USDUAH;
public Double USDUGX;
public Double USDUYU;
public Double USDUZS;
public Double USDVEF;
public Double USDVES;
public Double USDVND;
public Double USDVUV;
public Double USDWST;
public Double USDXAF;
public Double USDXAG;
public Double USDXAU;
public Double USDXCD;
public Double USDXDR;
public Double USDXOF;
public Double USDXPF;
public Double USDYER;
public Double USDZAR;
public Double USDZMK;
public Double USDZMW;
public Double USDZWL;
}
public static reponseWrapper parse(String json) {
return (reponseWrapper) System.JSON.deserialize(json,
reponseWrapper.class);
}
}
Step 4 :
Create a scheduled flow that runs every day at 6 AM.
Conclusion :
- The Dated Exchange Rate object will get the exchange rate once every day
- With the most recent api version orgs, this capability can also be accomplished in flow without any apex classes.