- 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 07:25 AM
UUID format is this -> The most used format is the 8-4-4-4-12 format, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
what's your business requirement?
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 07:39 AM
Hi @Ankur Bawiskar
Thanks for your reply,
GUID has 32 characters and
UUID also has 32-characters (8-4-4-4-12=32)
Business Use case is : we have REST API call to fetch customers data from database. in that API traceid is one http parameter. We(ServiceNow) have been asked by API owned team to pass unique value in traceid parameter , that value should be UUID format.
So that if anytime API request call get's failed at that time API team will track the call history by using this traceId.
For that we followed below format. but it is always giving "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'); }
How can we fix this issue, please suggest me/share some inputs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2025 08:02 AM
since you need to pass a unique UUID always you are on right track
Do this
1) get the sysId using gs.generateGUID()
2) then convert that to UUID format
3) use this to send in the traceid parameter
Sample script below
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;
}
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:14 AM - edited 04-17-2025 08:30 AM
how to test that uuid is valid or not.?
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'); }