Setting Integer in Variable Saves as Decimal/Float

ParkPants
Tera Contributor

Hi all,

I need help with a script that's been bugging me. I am working on a service portal catalog item that assigns a number that is incrementally generated for each successful submission. The number field is read-only and viewable only on TASKs that are generated from the RITM. The problem I'm having is that on incrementing a previous value and assigning it back to the variable, the number I know should be an integer is actually getting saved as a floating point number.

Expected Result:

grp_seq = 15; line_nbr = 15

Actual Result:

grp_seq = 15.0; line_nbr = 15.0

var number;
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('someQuery');
gr.query();
if (gr.next()) {
	number = parseInt(gr.variables.grp_seq.toString());
	if (number == "") {
                /*Initialize the sequence if first submission*/
		current.variables.grp_seq = 1;
		current.variables.line_nbr = 1;
	}
	else {
                /*Increment for each successful submission*/
		var newNum = parseInt(number) + 1;
		current.variables.grp_seq = newNum;
		current.variables.line_nbr = current.variables.grp_seq;
	}
}
1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Try adding toString() to the setting of the variables:

current.variables.grp_seq = newNum.toString();
		current.variables.line_nbr = current.variables.grp_seq.toString();

That worked for me.

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron
Hi, maybe it's a result of number being declared with out a defined type. I have often seen undeclared types interpreted as strings regardless of the value being set. Have you tried forcing by setting a default value, var number = 0; or maybe var number = 1; followed by number = 0; may work.

Willem
Giga Sage
Giga Sage

Try adding toString() to the setting of the variables:

current.variables.grp_seq = newNum.toString();
		current.variables.line_nbr = current.variables.grp_seq.toString();

That worked for me.

ParkPants
Tera Contributor

Awesome. That worked. Thanks for the help.