Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Help with script in flow designer: Fetching a number from sys_user and adding 1

MarinaN
Tera Expert

I need to query sys_user, where a custom string field u_g_number is located. This has a format of "G"+5 digits. 

I need to get the 5 digits, treat them as a number and add 1. This script is my script:

var maxNum = 0; // keep track of the highest numeric value found

var gr = new GlideRecord('sys_user'); // gr on sys_user
gr.orderByDesc('u_g_number');
gr.setLimit(1);
gr.query();


if (gr.next()) { // move to the first record if one exists
    var raw = (gr.u_g_number + '').trim(); // read number, remove space (not really needed but just to be safe)
    var match = raw.match(/^G(\d+)$/i);    // capture all digits after g
    if (match) {                          
        maxNum = parseInt(match[1], 10);   // convert the captured digits to a number
    }
}

var nextNum = maxNum + 1; // add 1 to the largest found number to get the next one
var nextG = 'G' + nextNum; 

gs.log('Flow generated next u_g_number: ' + nextG, 'UGen');
return nextG; 

 

No number is being generated, any idea why? 

I have an action "create user record" and next to the field u_g_number i added this script (works for another script above where we generate the email)

1 ACCEPTED SOLUTION

MarinaN
Tera Expert

There was no issue with my code - BUT:

 

Since I am looking through the sys_user table I needed to run the flow as "system user"!

 

It works now

View solution in original post

3 REPLIES 3

Martin Virag
Mega Sage
Mega Sage

okay, there are several issues with this script (I would not even use a script its a simple lookup step and then you can use a flow variable to do your operation) , however by fixing your script:
I assume the G is always the beginning in this way its perfectly fine to just use 
the .slice() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

Other reference is here: https://www.geeksforgeeks.org/javascript/delete-first-character-of-a-string-in-javascript/

Then use the parseInt to make it a number

Finally add +1

Here is the code:

var gr = new GlideRecord('sys_user');
gr.orderByDesc('u_g_number');
gr.setLimit(1);
gr.query();

var nextG;
if (gr.next()) {
    var num = parseInt(gr.u_g_number.slice(1), 10);
    nextG = 'G' + (num + 1);
}

gs.log('Flow generated next u_g_number: ' + nextG, 'UGen');
return nextG;



Ankur Bawiskar
Tera Patron
Tera Patron

@MarinaN 

try this

var maxNum = 0;
var gr = new GlideRecord('sys_user');
gr.addQuery('u_g_number', 'STARTSWITH', 'G');
gr.query();
while (gr.next()) {
    var raw = (gr.getValue('u_g_number') + '').trim();
    var match = raw.match(/^G(\d{5})$/); // captures exactly 5 digits
    if (match) {
        var num = parseInt(match[1], 10);
        if (num > maxNum) maxNum = num;
    }
}
var nextNum = maxNum + 1;
var nextG = 'G' + ('00000' + nextNum).slice(-5); // pads leading zeroes if needed
gs.log('Flow generated next u_g_number: ' + nextG, 'UGen');
return nextG;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

MarinaN
Tera Expert

There was no issue with my code - BUT:

 

Since I am looking through the sys_user table I needed to run the flow as "system user"!

 

It works now