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 

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.

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

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

@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

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

 

@Supriya25 

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.

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