Tuesday, June 26, 2018

Convert LDAP attribute objectGUID from base64 to string in ServiceNow LDAP integration - Transform script

answer = (function transformEntry(source) {

function toHex(d) {
    return ("0" + Number(d).toString(16)).slice(-2).toUpperCase();
}

function a2b(a) {
 var b, c, d, e = {},
 f = 0,
 g = 0,
 h = "",
 i = String.fromCharCode,
 j = a.length;
 for (b = 0; 64 > b; b++) e["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(b)] = b;
 for (c = 0; j > c; c++)
  for (b = e[a.charAt(c)], f = (f << 6) + b, g += 6; g >= 8;)((d = 255 & f >>> (g -= 8)) || j - 2 > c) && (h += i(d));
 return h;

}

function arrayToGuid(arr) {
      return arr.slice(0, 4).reverse().join('').toLowerCase() + '-' + arr.slice(4, 6).reverse().join('').toLowerCase() + '-' + arr.slice(6, 8).reverse().join('').toLowerCase() + '-' + arr.slice(8, 10).join('').toLowerCase() + '-' + arr.slice(10, 16).join('').toLowerCase();
}

function base64StringToGuid(base64string) {
    var charArray = GlideStringUtil.base64Decode(base64string).split('').map(function(el) {
      return el.charCodeAt(0);
    }).map(function(el) {
      return toHex(el);
    });
    return arrayToGuid(charArray);
}

return base64StringToGuid(source.u_objectguid); // return the value to be put into the target field

})(source);