How to validate UUID value in Server script

Supriya25
Tera Guru

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

 

1 ACCEPTED SOLUTION

@Supriya25 

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:

AnkurBawiskar_0-1744904286863.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

@Supriya25 

gs.generateGUID() -> gives you 32 char sysId
This is not in UUID format. So your script won't work

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.

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

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.

@Supriya25 

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:

AnkurBawiskar_0-1744902125196.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar 

Supriya25_0-1744903810658.png

 


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