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

terrillc
Tera Contributor

were you able to find the solution?

Hi @terrillc 


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 .