- 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 03:32 AM
Thanks everyone.
Seems the object of the class was not getting instantiated in the business rule, so did it using :
var obj = new ConvertToUSD();
while (gr_req_model.next()) {
actual_cost += parseFloat(obj.convert(gr_req_model.u_currency_code.toString(), gr_req_model.u_cost.toString()));
}
I still need to check why this is happening.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2020 03:55 AM
using a fix script or background script, run the query with sample data and see if the query works as expected. Could be a data conversion issue?