Salesforce Integration with Foreign Exchange Rates API for Currency Conversion

This blog is to create an Apex code that Integrates Salesforce with an external Exchange Rates API. This Apex code converts USD to INR. The base currency in this API is Euro, so we have to convert the Dollars to Euro and then to Indian Rupees.
Exchange rates API is a free service for current and historical foreign exchange rates published by the European Central Bank.

Exchange Rates Api integration with salesforce
Exchange Rates API



We will be using the HTTP request and response functionality.
Http Class: Use this class to initiate an HTTP request and response.
HttpRequest Class: Use this class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.
HttpResponse Class: Use this class to handle the HTTP response returned by HTTP.



Step 1 ==> Connect
Register the endpoint URL
Before your Apex code can call the exchangeratesapi endpoint, you must register the endpoint URL in the Remote Site Settings page.
1) Log in to Salesforce.
2) From Setup, enter Remote Site in the Quick Find box, then select Remote Site Settings.
3) Click New Remote Site.
4) For Remote Site Name, type exchange_rate.
5) For Remote Site URL, type https://api.exchangeratesapi.io/latest.
6) Click Save.


Step 2 ==> Create
Create ConvertExchangeRates Class 
1) Open the Developer Console under Your Name or the quick access menu (Setup gear icon).
2) In the Developer Console, click File | New | Apex Class, and enter ConvertExchangeRates for the class name, and then click OK.
3) Replace the default class body with the below code.


public class ConvertExchangeRates {

    public static Decimal usdToInr(Decimal dollar) {
        // Endpoint to be used using the string url
String url = 'https://api.exchangeratesapi.io/latest'; //?symbols=USD,INR;
Decimal INR = 0.0;
        // Instantiate a new http object
        Http h = new Http();
        
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        
        // If the request is successful, parse the JSON response.
        if(res.getStatusCode()==200){
            
            // Deserialize the JSON string into collections of primitive data types.
            Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
            Map<String,Object> value = (Map<String,Object>)results.get('rates');
            Decimal usdRate = (Decimal)value.get('USD');
            Decimal inrRate = (Decimal)value.get('INR');
            INR = (dollar/usdRate) * inrRate;
            System.debug('Rates of INR: '+INR);
            return INR;
        }
        return res.getStatusCode();
    }
}



Step 3 ==> Test
1) In the Developer Console, click Debug | Open Execute Anonymous Window.
2) If there is already code in the Enter Apex Code window, replace it with this code:

    ConvertExchangeRates.usdToInr(1000);

3) Select Open Log.
4) Click Execute. The execution log opens, displaying the result of running your code.
5) Select Debug Only. The Details column displays the contents of the debug statements in your code.
The DEBUG messages should contain the correct exchange rates in INR.


Resources
https://exchangeratesapi.io/ 


Hope this helps you...Happy Trailblazing..!


Comments

  1. {error={code=101, info=You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY], type=missing_access_key}, success=false}
    i am getting this error where should i put AccessKey

    ReplyDelete

Post a Comment

Popular posts from this blog

Administrator Certification Maintenance (Spring '21) Questions

Salesforce App Builder - Question Set 5