For building a Currency conversion function , we will be needing exchange rates on an hourly basis to be stored in SFDC. Is this going to be a manual task or we can automate the population of exchange rates? Does SFDC provide any out of the box functionality for currency conversion?
You can create a Apex job on hourly basis to fetch the exchange rate and store in a object as below.
CurrencyConverter is the class which fetch the conversion rate from yahoo and Store in a custom object called Rate. There are two fields one is Conversion_rate and Date. You can add more fields as required. I have used USD > INR conversion. You can update your currency in the URL parameter.
CurrencyConverterScheduler is the scheduler class which needs to be scheduled as per your requirement.
Apex Class - CurrencyConverter
global class CurrencyConverter{
@future(callout=true)
public static void convertCurrency(){
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
String endpoint = 'http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDINR%22%29&format=json&env=store://datatables.org/alltableswithkeys';
request.setEndPoint(endpoint);
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
JSONParser parser = JSON.createParser(response.getBody());
String convRate = '';
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Rate')) {
parser.nextToken();
convRate = parser.getText();
}
}
System.debug('convRate :'+convRate);
Rate__c rate = new Rate__c();
rate.Conversion_rate__c = convRate;
rate.Date__c = Date.today();
insert rate;
}
}
Apex Class - CurrencyConverterScheduler
global class CurrencyConverterScheduler implements Schedulable {
global void execute(SchedulableContext ctx){
CurrencyConverter.convertCurrency();
}
}
-
My requirement would be to convert any currency to either Euro or USD . Also can you please let me know the exact yahoo portal from where exchange rates can be fetched . I have been searching that in vain with forums telling me that yahoo finance has only stock info. – Vanilla_Sky May 14 '15 at 9:29
-
This is the Yahoo currency conversion site link where you can get all the exchange rates. – Saroj Bera May 14 '15 at 10:49
-
To use http rest API follow the steps below 1 Start at the YQL Console link 2 In the upper left corner, make sure Show Community Tables is checked and JSON is selected in middle. 3 Type yahoo.finance.xchange in the search field and click on the result. 4 You'll see a query in the field YOUR YQL STATEMENT as select * from yahoo.finance.xchange where pair in ("EURUSD","GBPUSD") 5 You will get the HTTP REST URL in the field THE REST QUERY which I have used in the CurrencyConverter class. – Saroj Bera May 14 '15 at 10:50
SFDC does not provide this functionality out of the box. There are apps in the AppExchange, however, that will do that. Please look for yourself, I don't have any experience with these myself.