- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 01:59 AM
I have created a script include to convert currency to USD which takes the latest Currency rates and convert the input values to usd value. It takes 2 inputs
- currencyCode (which needs to be converted e.g. INR or GBP)
- currencyValue
var ConvertToUSD = Class.create();
ConvertToUSD.prototype = {
initialize: function() {},
convert: function(currencyCode, currencyValue) {
var rate;
var USDValue;
var rateUSD = this.rateUSD();
var gr_currency = new GlideRecord('fx_currency');
gr_currency.addQuery('code', currencyCode);
gr_currency.query();
if (gr_currency.next()) {
var gr_rate = new GlideRecord('fx_rate');
gr_rate.addQuery('currency', gr_currency.getUniqueValue());
gr_rate.orderByDesc('sys_created_on');
gr_rate.setLimit(1);
gr_rate.query();
if (gr_rate.next()) {
rate = parseFloat(gr_rate.rate);
USDValue = parseFloat(currencyValue) * (rateUSD) / (rate);
}
}
return USDValue.toString();
},
rateUSD: function() {
var rateUSD;
var gr_rate_USD = new GlideRecord('fx_rate');
gr_rate_USD.addQuery('currency', 'USD');
gr_rate_USD.orderByDesc('sys_created_on');
gr_rate_USD.setLimit(1);
gr_rate_USD.query();
if (gr_rate_USD.next()) {
rateUSD = parseFloat(gr_rate_USD.rate);
}
return rateUSD;
},
type: 'ConvertToUSD'
};
But when I call it in a business rule it is returning an undefined value.
actual_cost += parseFloat(ConvertToUSD.convert(gr_req_model.u_currency_code.toString(),gr_req_model.u_cost.toString()));
Any suggestions on what I am doing wrong?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 03:21 AM
Hi Anirudh,
Please do small correction after the class name we have specify the ().
actual_cost += parseFloat(new ConvertToUSD().convert(gr_req_model.u_currency_code.toString(),gr_req_model.u_cost.toString()));
Regards,
Bala T
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 02:20 AM
Hi There,
what is gr_req_model containing, i believe while passing the values to your script include it is sending undefined values.
Try adding gs.log('the value of '+gr_req_model.u_currenct_code);
and check are you getting the proper value here or not
same way check for cost also.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 02:45 AM
I have tried checking it and it seems these both are string values.
while(gr_req_model.next()) {
gs.info('Code: '+ gr_req_model.u_currency_code.toString() + typeof(gr_req_model.u_currency_code.toString()));
gs.info('Cost: '+ gr_req_model.u_cost.toString() + typeof(gr_req_model.u_cost.toString()));
gs.info( ConvertToUSD.convert(gr_req_model.u_currency_code.toString(),gr_req_model.u_cost.toString()));
}
The output of this is basically :
** Script: Code: USDstring
*** Script: Cost: 52.00string
*** Script: undefined
*** Script: Code: USDstring
*** Script: Cost: 108.00string
*** Script: undefined
*** Script: Code: USDstring
*** Script: Cost: 40.00string
*** Script: undefined
so it seems I am passing the string to the class.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 02:48 AM
Hi
Can you calling script include with new,
like this -
actual_cost += parseFloat(new ConvertToUSD.convert(gr_req_model.u_currency_code.toString(),gr_req_model.u_cost.toString()));
Let me know if it works.
Regards
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 03:21 AM
Hi Anirudh,
Please do small correction after the class name we have specify the ().
actual_cost += parseFloat(new ConvertToUSD().convert(gr_req_model.u_currency_code.toString(),gr_req_model.u_cost.toString()));
Regards,
Bala T