Integer field is removing the leading zeroes of a number

Nani7
Tera Contributor

Experts,

I'm facing an issue with the integer type field type which is removing the leading zeroes of a number.

Example, if I enter 004498, it is getting updated as "4498".

I have to do an action that would need to increment this value using a server side script. So, if a run a BR, this value should be updated as "004499" and not "4499".

Any suggestions or alternatives?

- Nani

 

1 ACCEPTED SOLUTION

Hi,

this should work fine

var currentValue = "002100";
gs.info("Current Value: " + currentValue);

var zeroArray = [];

for(var i=0;i<currentValue.length;i++){

if(parseInt(currentValue.charAt(i)) == 0){
zeroArray.push(currentValue.charAt(i).toString());
continue;
}
else{
break;
}
}

var number = currentValue.replace(/^0+/, ''); // remove leading zeros
gs.info("Number: " + number);

number = parseInt(number) +1; // increment by 1

var finalValue = zeroArray.toString().replaceAll(',','') + number;

gs.info("Final Value: " + finalValue);

Output:

[0:00:00.084] Script completed in scope global: script


Script execution history and recovery available here


*** Script: Current Value: 002100
*** Script: Number: 2100
*** Script: Final Value: 002101

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Hi,

you need to get all the leading zeros

in your case for 010000 it is 1 zero

so after incrementing you can add only that 1 zero in the leading string

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Okay... That makes sense... Can you help me with the procedure to fetch the leading zeroes in the script? Would be helpful for my reference!

Hi,

something like this

var currentValue = current.u_field.toString();

var zeroArray = [];

for(var i=0;i<currentValue.length;i++){

if(parseInt(currentValue.charAt(i)) == 0){

zeroArray.push(currentValue.charAt(i).toString());

}

}

var number = currentValue.replace(/^0+/, ''); // remove leading zeros

number = parseInt(number) +1; // increment by 1

var finalValue = zeroArray.toString() + number; // join the zero array and the number

current.u_field = finalValue;

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

var currentValue = "002100";
gs.print("Current Value: " + currentValue);


var zeroArray = [];

for(var i=0;i<currentValue.length;i++){

if(parseInt(currentValue.charAt(i)) == 0){

zeroArray.push(currentValue.charAt(i).toString());

}

}

var number = currentValue.replace(/^0+/, ''); // remove leading zeros
gs.print("Number: " + number);

number = parseInt(number) +1; // increment by 1

var finalValue = zeroArray.toString() + number; // join the zero array and the number


gs.print("Final Value: " + finalValue);


/*Output a follows
*** Script: Current Value: 002100
*** Script: Number: 2100
*** Script: Final Value: 0,0,0,02101
*/

Hi,

make this change and it work fine

var finalValue = zeroArray.toString().replaceAll(',','') + number; // join the zero array and the number

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader