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

@Supriya25 

Hope you are doing good.

Did my reply answer your question?

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

miyelii
Tera Contributor

This is not in UUID format.

Hi 

is it NOT  UUID format ? please let me know if something wrong happing here.

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;
}
*** Script: UUID: 1ba32558-8301-2210-769a-c020feaad329
*** Script: UUID: true

Could you please check above  script once .


@Supriya25 

I already answered your question

I informed how to convert sysId to UUID format

Why are you trying to validate it again?

Your task is simply generate random sysId, convert to UUID format and include in API for 3rd party team

it's like you are performing 2+2=4 via script and then again validating if 2+2=4 or not

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

Yes, I good.
I thought of if any additional validations need to be covered over there.