Increment a string field that ends with a number by 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 07:14 PM
Hi,
We have a form automation that creates a new server. One of the requirements is to check to see if the server requested has already been created in our CMDB and if it already exist then check for the next available number.
Our servers have a naming convention like the following:
CYSQLSNT01
If a server already exist like the one above, then they want me to increment the last numbers of the name. So the next would be CYSQLSNT02 or CYSQLSNT03.
Based on the requirements, the last 2 characters of the server name will always be a 2 digit number.
How can I increment this number? ParseInt didn't work. It came us as NaN.
I also need to make sure that the numbers start with 0, if it is one number. For instance, if the number is 5 then it would be 05 or if the number is 16, then it would just be 16. The last 2 characters are 2 digit numbers.
Any help would be appreciated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2023 10:48 AM
Hi,
You can try something like the code below (not optimized or anything, but it works), and I assume you know how to get the current server with the highest number in your system,
var serverName = 'CYSQLSNT03';
var digits = serverName.substring(serverName.length-2); // assuming there are always only 2 last characters as digits in the name
gs.info('Digits: ' + digits);
digits = parseInt(digits, 10); // convert from string to number
digits = digits + 1;
gs.info('Digits increased: ' + digits);
if (digits == 99){
// some alert here maximum number reached
}
var newServerName = serverName.substring(0, serverName.length-2); // again assuming only 2 last chars are digits
gs.info('name without digits: ' + newServerName);
var addDigitsString = '';
if (digits < 9){
addDigitsString = '0' + digits.toString();
}
else{
addDigitsString = digits.toString();
}
newServerName = newServerName + addDigitsString;
gs.info('new name: ' + newServerName);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 05:37 PM
Hi @Dazler did you find a solution for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 06:05 PM - edited 10-05-2023 06:07 PM
Hi @yoli1
Yes, I used in mine in flow by creating an action.
I used this new action within the Do the following flow action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 02:17 PM - edited 10-08-2023 05:03 PM
Hi @Dazler i'am new to flowdesigner can you please share the flow you used Thanks a lot! i 'am really struggling here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 04:06 AM - edited 10-09-2023 04:24 AM
@Dazler this is my code :
(function execute(inputs, outputs) {
var baseName = inputs.locCode + inputs.operatingSystem + inputs.formFactor;
var gr = new GlideRecord('cmdb_ci_server'); // Replace with your actual table name
// Query the existing servers that match the base name
gr.addQuery('name', 'STARTSWITH', baseName);
gr.query();
// Initialize the next server number
var nextServerNumber = 1;
// Find the highest existing number and increment it
while (gr.next()) {
var currentServerName = gr.name.toString();
var numberPart = currentServerName.replace(baseName, '');
var currentNumber = parseInt(numberPart, 10);
if (!isNaN(currentNumber) && currentNumber >= nextServerNumber) {
nextServerNumber = currentNumber + 1;
}
}
var incrementedBaseName = baseName + nextServerNumber.toString().padStart(2, '0');
outputs.newservername = incrementedBaseName;
})(inputs, outputs);
in the output if the name is CYSQLSNT01 i get CYSQLSNTundefined
@Dazler Your code makes sense, but I'm not sure how to only take the numeric value from the name to increment it.