Useful Scripts

In my years as a developer, I’ve collected a large number of abstract, reusable scripts for various purposes. This practice has saved me more time and headaches than almost anything else I’ve done as a developer, because just about every interesting problem I’ve solved with code, I have inevitably had to solve twice.

On this page, you’ll find a few of the scripts I’ve saved over the years, and maybe a few scripts submitted by our most clever and dashing readers. The scripts you’ll find here, are one-off blocks of code that solve one problem as efficiently as they can. You won’t find detailed deep-dive explanations here, like you would in my articles on the blog; just examples, prototypes, and quick fixes to common coding conundrums. You also won’t find any of the scripts that I’ve already written articles with detailed explanations on, such as the Journal Redactor.

Most of these scripts will be specific to ServiceNow, but not all. Some are pure-JS. Some solve problems in ES5 that could easily be solved if we had ES6 on the server. Some aren’t even clever, but I use them so frequently that I figured I’d write them down.

This page is still under construction, so you’ll only find a few scripts here for now. Keep an eye on this page over the next few months, as I expand upon this repository. If you’d like to contribute your own scripts (and of course, be credited), please send me an email with your script, what it does, what problem it solves, and any other info you think I ought to know about it. Don’t forget to also let me know how you’d like to be credited (your name, and if you’d like, your website that I’ll link to).


  1. Retrieve Decrypted Field Value from Password2 Field

  2. Add One Object to Another Object or Array

  3. Get Catalog Item Variables (Organized Into Containers)

  4. Get URI Parameters (Portal & Classic UI)

  5. Check if a Specific User Has Access to Something

  6. Check if a Given String Contains Any of a List of Values

  7. Query a Base Table - Get a Record/Fields on an Extended Table

  8. Use Different Credentials/Mid-servers/Endpoints if Instance is Prod Vs. Sub-Prod


Retrieve Decrypted Field Value from Password2 Field

    The following example retrieves and prints the value of
     a password2 from a basic auth profile, with the sys_id:
    Be careful with this! Passwords are obviously SENSITIVE!
var encryptedPassword,
    Encrypter = new GlideEncrypter(),
    grBasicAuthProfile = new GlideRecord('sys_auth_profile_basic');

encryptedPassword = grBasicAuthProfile.getValue('password');

decryptedPassword = Encrypter.decrypt(encryptedPassword);


/*****Client-callable example*****/
var PasswordDecryptor = Class.create();
PasswordDecryptor.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getPasswordValue: function() {
        if (!gs.hasRoleExactly('admin')) {
        var decryptedPassword;
        var tableName = this.getParameter('sysparm_table');
        var recordID = this.getParameter('sysparm_record_id');
        var fieldName = this.getParameter('sysparm_field_name');
        var geDecryptor = new GlideEncrypter();
        var grCredential = new GlideRecord(tableName);
        decryptedPassword = geDecryptor.decrypt(
        return decryptedPassword;

/*****CLIENT SCRIPT*****/
function onLoad() {
    if (g_user.hasRoleExactly('admin')) {
        var gaDecryptor = new GlideAjax('PasswordDecryptor');
        gaDecryptor.addParam('sysparm_name', 'getPasswordValue');
        gaDecryptor.addParam('sysparm_table', g_form.getTableName());
        gaDecryptor.addParam('sysparm_record_id', g_form.getUniqueValue());
        gaDecryptor.addParam('sysparm_field_name', 'password'); //replace with password field name
     * @param response
    function cbShowPassword(response) {
        var decryptedPassword = response.responseXML.documentElement.getAttribute('answer');
            'password', //Replace with password field name

Add One Object to Another Object or Array

 * Adds one object to another, nesting the child object into the parent.
 * this is to get around javascript's immutable handling of objects.
 * @param name {String} - The name of the property of the parent object, in which to nest the child object. <br />For example, if the name parameter is set to "pickles" then "parent.pickles" will return the child object.
 * @param child {Object} - The object that should be nested within the parent object.
 * @param [parent={}] {Object} - The parent object in which to nest the child object. If the parent object is not specified, then the child object is simple nested into an otherwise empty object, which is then returned.
 * @returns {Object} - A new object consisting of the parent (or an otherwise empty object) with the child object nested within it.
 * @example
 * //sets myNewObject to a copy of originalObject, which now also contains the original (yet un-linked) version of itself as a child, under the property name "original"
 * var myNewObject = addObjToObj("original", originalObj, originalObj);
function addObjToObj(name, child, parent) {
    if (!parent) {
        parent = {};
    parent[name] = child;
    return parent;

//Here's another way you can add an object to an array or another object -- break the "pass-by-reference" model, by stringifying and then parsing the object.
function addObjToArray(arr, obj) {
    return arr;

//And here's an object that has a method for adding other objects to itself!
var myObj = {
    addObject: function(propName, obj) {
        this[propName] = JSON.parse(JSON.stringify(obj));

//Remember that arrays are just numerically indexed objects, so this all works with them too!

Get Catalog Item Variables (Organized Into Containers)

    fieldName = 'sys_id',
    varMap = {},
    containerLevel = [],
    item = GlideappCatalogItem.get(CATALOG_ITEM_SYS_ID),
    grVariables = item.getVariables();

//For every variable found...
while ( {

    var varName = grVariables.getValue('name');
    var varSid = grVariables.getValue('sys_id');
    var varType = grVariables.type.getDisplayValue();
    var varQuestion = grVariables.getValue('question_text');
    var varActive = grVariables.getValue('active');

    if (varType === 'Container Start') {
    } else if (varType === 'Container End') {
    currentContainer = varMap;
    for (i = 0; i < containerLevel.length; i++) {
        if (!currentContainer.hasOwnProperty(containerLevel[i])) {
            currentContainer[containerLevel[i]] = {};
        currentContainer = currentContainer[containerLevel[i]];

    currentContainer[varSid] = new Variable(varName, varSid, varType, varQuestion, varActive);


function Variable(varName, varSid, varType, varQuestion, varActive, varOrder) {
    this.varName = varName;
    this.varSid = varSid;
    this.varType = varType;
    this.varQuestion = varQuestion;
    this.varActive = varActive;
    this.varOrder = varOrder;

Get URI Parameters (Portal & Classic UI)

//Note: Read from innermost, outward
        getAllUrlParams( //Get all URL params. Since SN re-encodes everything it passes into page processors like "" for example, this will have one key-val pair.
                         (this.location.href ? this.location.href : window.location.href) //The document URL. Should work for SP, and desktop view.

var yourParamValue = getAllUrlParams(this.location.href)['YOUR_PARAM_NAME'];

 * Get the values of each URI param as properties of an object (name-value pairs).
 * @param url
 * @returns {{}}
 * @example - In the URL, you could use getAllUrlParams().urip to get "pickles".
function getAllUrlParams(url) {
    // get query string from url (optional) or window
    var queryString = url ? url.split('?')[1] :;
    // we'll store the parameters here
    var obj = {};
    // if query string exists
    if (queryString) {
        // stuff after # is not part of query string, so get rid of it
        queryString = queryString.split('#')[0];
        // split our query string into its component parts
        var arr = queryString.split('&');
        for (var i = 0; i < arr.length; i++) {
            // separate the keys and the values
            var a = arr[i].split('=');
            // in case params look like: list[]=thing1&list[]=thing2
            var paramNum = undefined;
            var paramName = a[0].replace(/\[\d*\]/, function(v) {
                paramNum = v.slice(1, -1);
                return '';
            // set parameter value (use 'true' if empty)
            var paramValue = typeof(a[1]) === 'undefined' ? true : a[1];
            // (optional) keep case consistent
            paramName = paramName.toLowerCase();
            paramValue = paramValue.toLowerCase();
            // if parameter name already exists
            if (obj[paramName]) {
                // convert value to array (if still string)
                if (typeof obj[paramName] === 'string') {
                    obj[paramName] = [obj[paramName]];
                // if no array index number specified...
                if (typeof paramNum === 'undefined') {
                    // put the value on the end of the array
                // if array index number specified...
                else {
                    // put the value at that index number
                    obj[paramName][paramNum] = paramValue;
            // if param name doesn't exist yet, set it
            else {
                obj[paramName] = paramValue;
    return obj;

Check if a Specific User Has Access to Something

var secManager = new GlideSecurityManager.get().setUser(gs.getUser().getUserByID('user_sys_id'));
var checkType = 'record';
var checkTable = 'incident';
var checkOperation = 'read';
var userSysID = '' //todo: Specify user Sys ID
if (hasRights(checkType, checkTable, checkOperation, userSysID)) {
    //todo: do stuff if the user has permissions
} else {
    //todo: do stuff if the user doesn't have permissions

function hasRights(checkType, checkTable, checkOperation, userSysID) {
    var secManager = new GlideSecurityManager.get().setUser(gs.getUser().getUserByID(userSysID));
    return secManager.hasRightsTo(
        checkType + '/' + checkTable + '/' + checkOperation //This string has the format ['record', 'table' or 'field']/[table name, such as 'incident']/[operation: read/write/delete/report]

Check if a Given String Contains Any of a List of Values

/**Checks whether a given string contains any of the values in a specified array or comma-separated list.
 * @param stringToCheck {string} The string to check against the list of values to check for.
 * @param listOfValuesToCheckFor {object|string} An array or comma separated string, containing a list of strings to check. If any of these strings
 * @returns {object} An object with two properties: answer (a boolean indicating whether any string from the second arg was found in the first) and values (an array of the values from the second arg, that were found in the first).
function stringContains(stringToCheck, listOfValuesToCheckFor) {
    var i;
    var response = {
        answer: false,
        values: []
    //If the subProdKeywords variablr is a string with at least one character, convert it to an array.
    if (typeof listOfValuesToCheckFor == 'string' && listOfValuesToCheckFor.length > 0) {
        listOfValuesToCheckFor = listOfValuesToCheckFor.split(',');
    else if (typeof listOfValuesToCheckFor !== 'object' || !(listOfValuesToCheckFor.length > 0)) {
        //If the first condition (subProdKeywords is a string) is NOT met, then we expect an object (array) with at least one value.
        //If either of the mentioned conditions is not met and the value was not a string, then we return undefined and halt execution while logging the error.
        gs.warn('Invalid value or data type passed into stringContains() method, or value is null-length. Value passed in: ' + listOfValuesToCheckFor);
        return undefined;
    for (i = 0; i < listOfValuesToCheckFor.length; i++) {
        if (stringToCheck.indexOf(listOfValuesToCheckFor[i])) {
            response.answer = true;
    return response;

Query a Base Table - Get a Record/Fields on an Extended Table

var grChildTable;
var childFieldValues = [];
var baseTable = 'cmdb_ci'; //A base table that's extended
var childTableFieldName = 'nic'; //The name of a field on an extended table
var grBaseTable = new GlideRecord('cmdb_ci');


while ( {
    grChildTable = new GlideRecordUtil().getGR(
        grBaseTable.getValue('sys_class_name'), //Table name 
        grBaseTable.getValue('sys_id') //sys_id

Use Different Credentials/Mid-servers/Endpoints if Instance is Prod Vs. Sub-Prod

 * Checks the glide.installation.production property and returns true if the instance is production. <br/>
 * Also checks the instance name, and logs if there is a discrepancy between the instance name and glide.installation.production property.
 * @param [defaultValue=true] {boolean}
 * @returns {*}
function isProd(defaultValue) {
    //set default value for defaultValue parameter to true
    defaultValue = (typeof defaultValue !== 'undefined') ? defaultValue : true;
    var prodProp = isProdPropTrue(defaultValue);
    var instanceNameProd = isInstanceNameProd();
    if ((prodProp && !instanceNameProd) || !prodProp && instanceNameProd) {
            'isProd Script Include'
    return prodProp;
    function isProdPropTrue(defaultValue) {
        //Hey look, JS's lack of strict typing comes in handy for once!
        //even if only to fix problems that it causes...
        var thingsThatAreTrue = [true, 'true', 1, '1'];
        var isProdProp = gs.getProperty('glide.installation.production', defaultValue);
        return (thingsThatAreTrue.indexOf(isProdProp) >= 0);
    function isInstanceNameProd() {
        var instanceName = gs.getProperty('instance_name', null);
        var isDev = (instanceName.indexOf('dev') >= 0);
        var isTest = (instanceName.indexOf('test') >= 0);
        //If you have OTHER sub-prod instances like "qa", add those conditions too
        return (!isDev && !isTest);