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 Ankur,

Looks like the script is considering all the 0s in the string (NOT JUST THE LEADING 0s).

Output:

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

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

@Nani 

Hope you are doing good.

Did my reply answer your question?

If so, please mark my response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

Thanks!
Ankur

 

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