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

Mohit Kaushik
Mega Sage
Mega Sage

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

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

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.

Omkar Mone
Mega Sage

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

 

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