How to convert other country currency to USD

jai281997
Tera Contributor

Hello,

i am trying to build one logic on my form,  i have 2 fields where we can give currency value

nominal currency&

currency in usd

jai281997_0-1753183814616.png

when i give any value in nominal field value it should automatically convert in USD and should populate in current in USD field 
can anyone please suggest me on this how to build the logic

Thank you!!

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@jai281997 

what do you mean by nominal currency? It should be some currency.

check how you can convert between currencies and then enhance the script for your requirement

fx_rate table stores the conversion value.

Something like this in script to convert USD to GBP

var inc = new GlideRecord('incident');
inc.get('8f54ac5207f9e014540bf2508c1ed034');

gs.info('USD value->' + inc.u_my_currency);

var targetCurrency = 'GBP';

var value = convert(inc.u_my_currency.getCurrencyCode(), inc.u_my_currency, targetCurrency);

gs.info('GBP Value->' + value);

function convert(currencySysId, amount, targetCurrency) {

    var currencySysId = currencySysId;

    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", targetCurrency);
        euroToUsdGr.query();
        if (euroToUsdGr.next()) {
            return (parseFloat(euroToUsdGr.rate) * selectedCurrencyToEuro).toFixed(2);
        }
        return ''; // no conversion found

    }
    return ''; // no conversion found

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar ,

 

I understood the code but what i think Rates are not static, those keep changing everyday with Market so converted value will be just  a nearby value(around) , not exact value. - Correct.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

@Nikhil Bajaj9 

that's correct, out of the box fx_rate table is updated I believe on daily basis

We can rely on that.

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

Hi @Ankur Bawiskar ,

 

Thanks for info.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj