Currency conversion to USD

Sandy32
Tera Contributor

I had requirement on currency conversion, we will enter amount in variable(single line text) and will select currency(reference--fx_currency) so, user will add amount like 1000,200.50 etc and will select currency then amount will convert with USD and populate in new variable.. Below is the screenshot for ref. Please help me on this requirement. 

find_real_file.png

1 ACCEPTED SOLUTION

Sample script below

You can then use GlideAjax and onChange Client Script

Input: currency sys_id, amount

Output: amount in USD

Script:

getConversionValue: function(){

		var currencySysId = this.getParameter('sysparm_currencySysId');
		var amount = this.getParameter('sysparm_amount');

		var selectedToEuroGr = new GlideRecord("fx_rate");
		selectedToEuroGr.orderByDesc('sys_created_on');
		selectedToEuroGr.addQuery("currency", currencySysId);
		selectedToEuroGr.query();
		if (selectedToEuroGr.next()) {
			var selectedCurrencyToEuro =  parseFloat(amount)/parseFloat(selectedToEuroGr.rate);

			selectedCurrencyToEuro = selectedCurrencyToEuro.toFixed(3);

			var euroToUsdGr= new GlideRecord("fx_rate");
			euroToUsdGr.orderByDesc('sys_created_on');
			euroToUsdGr.addQuery("currency.code", "USD");
			euroToUsdGr.query();
			if(euroToUsdGr.next()){
				return (parseFloat(euroToUsdGr.rate)*selectedCurrencyToEuro).toFixed(3);
			}
			return ''; // no conversion found

		}
		return ''; // no conversion found

	},

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

DevTools is a scoped application with many helpful scripts ready to be re-used.

The built-in currency conversion API object (GlideCurrencyConverter) is a bit tricky to use.

Here you can find a nice wrapper function for currency conversion based on the ServiceNow OOTB functionality:

https://github.com/saschawildgrube/servicenow-devtools/blob/master/update/sys_script_include_6f29745...

Arjun2
Tera Contributor

Hi @Ankur Bawiskar ,

Thank you for the script. It works for a few currencies. But for few of the currencies, the exchange rates do not get show up. Upon checking, I see, the fx_rate table does not have values for that currency (For eg. NGN, PAB). I also see that "ECB Exchange Rate Load" is active and running daily and updating other currency exchanges. 

Why are few currency exchange rates not updated/present?