- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 07:00 AM
Hi All,
Please help me how to validate UUID format in Server side script
below script always returning "Not Valid" .
var uuidValue= gs.generateGUID();
uuidValue=uuidValue.replace(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/ "$1-$2-$3-$4-$5");
var uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
if (uuidRegex.test(uuidValue)) {
gs.info('Valid');
} else {
gs.info('Not Valid');
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:38 AM
try this
var sysId = '8bafe2e4db072150966c676ed396194c'; // Replace with your sys_id
function convertSysIdToUUID(sysId) {
if (sysId.length !== 32) {
gs.info('Invalid sys_id length');
return null;
}
return sysId.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5');
}
var uuidValue = convertSysIdToUUID(sysId);
gs.info('UUID: ' + uuidValue);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:24 AM
why you want to validate?
your task is to simply convert sysId to UUID which is already working fine with the script I shared and is in the standard format
UUID is having this format which is universally accepted UUID also has 32-characters (8-4-4-4-12=32)
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:34 AM
your code giving Error , kindly check it once
Javascript compiler exception: missing ) after argument list (null.null.script; line 15) in: var sysId = gs.generateGUID(); var uuidValue = convertSysIdToUUID(sysId); gs.info('UUID Format value->' + uuidValue); function convertSysIdToUUID(sysId) { if (sysId.length !== 32) { gs.info('Invalid sys_id length'); return null; } var uuid = sysId.substring(0, + '-' + sysId.substring(8, 12) + '-' + sysId.substring(12, 16) + '-' + sysId.substring(16, 20) + '-' + sysId.substring(20); return uuid; }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:38 AM
try this
var sysId = '8bafe2e4db072150966c676ed396194c'; // Replace with your sys_id
function convertSysIdToUUID(sysId) {
if (sysId.length !== 32) {
gs.info('Invalid sys_id length');
return null;
}
return sysId.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5');
}
var uuidValue = convertSysIdToUUID(sysId);
gs.info('UUID: ' + uuidValue);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:48 AM
Thanks you , I have added additional validation part also , please check it once and share your inputs please.
var sysId = gs.generateGUID();
function convertSysIdToUUID(sysId) {
if (sysId.length !== 32) {
gs.info('Invalid sys_id length');
return null;
}
return sysId.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5');
}
var uuidValue = convertSysIdToUUID(sysId);
gs.info('UUID: ' + uuidValue);
gs.info('UUID: ' + isUUID(uuidValue));
function isUUID ( uuid ) {
var s = "" + uuid;
s = s.match('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
if (s === null) {
return false;
}
return true;
}
[0:00:00.028] Script completed in scope global: script
Script execution history and recovery available here
*** Script: UUID: 1ba32558-8301-2210-769a-c020feaad329
*** Script: UUID: true
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:45 PM
Looks good.
I believe I have answered your question on how to convert sysId to UUID
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader