- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2021 07:22 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 01:15 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 01:05 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 01:15 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2021 11:43 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader