Increment a string field that ends with a number by 1

Dazler
Mega Sage

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.

10 REPLIES 10

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);

yoli1
Tera Contributor

Hi @Dazler did you find a solution for this?

Hi @yoli1 

 

Yes, I used in mine in flow by creating an action. 

 

Screenshot 2023-10-05 200152.png

 

I used this new action within the Do the following flow action. 

 

Screenshot 2023-10-05 200726.png

yoli1
Tera Contributor

Hi @Dazler i'am new to flowdesigner can you please share the flow you used  Thanks a lot! i 'am really struggling here 

yoli1
Tera Contributor

@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.