Help with script in flow designer: Fetching a number from sys_user and adding 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
15 hours ago
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
15 hours ago
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12 hours ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader