Script returning undefined

Anirudh Gupta
Kilo Expert

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 

  1. currencyCode (which needs to be converted e.g. INR or GBP) 
  2. 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?

1 ACCEPTED SOLUTION

Baala T
Mega Guru

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

View solution in original post

6 REPLIES 6

Anirudh Gupta
Kilo Expert

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.

Sukraj Raikhraj
Kilo Sage

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?