I want to convert currency field

schd
Mega Contributor

Hi,

On ast_lease table, i want to convert currency field from USD is THB

Follow the image: I want to convert currency "Total cost" field from USD is THB on "Total(THB) field

How to config or write js ?

Picture1.png

Thanks,

Suchawadee

4 REPLIES 4

Geoffrey2
ServiceNow Employee
ServiceNow Employee

You're going to need an exchange rate - which changes every minute. So you'll either need to specify an average constant rate, or build a web service to get the current exchange rate at the time your code executes.



After you have the exchange rate, it's just simple maths operations.


Total (THB) = Total cost * exchange rate;


fkhan
Kilo Guru

Hi Homkhajorn,



It can be done by writing a UI Script and onChange client script on u_total_cost field.




UI Script:    


Name:Anything


Global:Marked True


Script:


                  GlideForm.prototype.setCurrencyField = function(field, currency, value) {


                            this.setValue(field+'.display', value);


                            this.setValue(field+'.currency', currency);


                            this.setValue(field, currency+';'+value);


                  }





Client Script:


Type: onChange


Field Name:Total Cost


Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


  return;


  }


  var USD=g_form.getValue('u_total_cost');


  alert(USD);


  var split=USD.split(';');


  var SPLIT=split[1];


  var COST =parseInt( SPLIT.replace(/,/,''));


  //assuming exchange rate is 53.83;


  var THB=COST*53.83;


  alert(THB);


  g_form.setCurrencyField('u_thb_cost',' £', THB);


}


find_real_file.png


Thanks,


Farukh


PS: Mark Helpful or correct if applicable......!!!!


Daniel Oderbolz
Kilo Sage

I know it is an old thred, but still:

There is no need to reinvent the wheel.

Service Now contains an exchange rate table (fx_rate) and a scheduled job that allows you to load the data:

https://docs.servicenow.com/bundle/istanbul-platform-administration/page/administer/currency/concept/currency.html

However, the API does not seem to expose a function to convert from one currency to another.
(This would be easy to provide and very beneficial).

Basically, using  said table you convert the original currency to EUR and then from EUR to the target.
(Note that this "double-conversion" might introduce an undue amount of rounding error, depending on your application. I would not use it for a banking application...)

Best
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

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...