logo

NJP

Utility for checking a parameter for a Method is a valid record for a table.

Import · Oct 12, 2023 · article

So there are many times when building Script Includes I need/want to check the values passed in to make sure that what I have is a GlideRecord or a valid SYS_ID for a table. Other times I want to make sure that my method can take either a GlideRecord or a SYS_ID as an input to make it more flexible. So I built the below Utility script include to allow me to do that without having to add something to every script include that I built. I thought I would share it for others to use if they so chose. Since it can make your Script Include more flexible in how you use them.

The code is below but I also attached an XML file that you can just import. There is also a simple example at the bottom.

Code:

var scriptIncludeUtil = Class.create();
scriptIncludeUtil.prototype = {
    initialize: function() {
    },

    type: 'scriptIncludeUtil'
};

//This method checks to see if the passed record is a GlideRecord for the table passed or
//a sys_id in the table passed.  If it is then the GlideRecord is returned.  This can be
//used to validate a passed value to a method or allow a sys_id or GlideRecord to be passed
//to a method and this will check it for the table and if its a sys_id in the table the
//record will be retrieved and returned.
scriptIncludeUtil.checkGetGlideRecord = function(record, table){

    if(scriptIncludeUtil.checkGlideRecord(record, table)){
        return record;
    } else if(scriptIncludeUtil.checkString(record)){
        var rec = new GlideRecord(table);
        if(rec.get(record)){
            return rec;
        } else {
            return null;
        }
    } else {
        return null;
    }

};

scriptIncludeUtil.checkGlideRecord = function(record, table){

    if(record && record instanceof GlideRecord && record.getTableName() == table){
        return record;
    } else {
        return null;
    }

};

scriptIncludeUtil.checkString = function(record){

    if(record && (typeof record == "string" || record instanceof String)){
        return true;
    } else {
        return false;
    }

};

Example:

For a simple example lets say you need a Utility that will set a user's Active or Inactive and you want to be able to pass in either a GlideRecord or a SYS_ID to the same method.

//Usage with just a SYS_ID
var sys_id = "64aced3cda4d18101c3d00621849a340";
userUtil.setActive(sys_id, false, true);

//Usage in a business run on the sys_user table
(function executeRule(current, previous /*null when async*/) {
    userUtil.setActive(current, false, false);
})(current, previous);

//********** Sample Script Include
var userUtil = Class.create();
userUtil.prototype = {
    initialize: function() {
    },

    type: 'userUtil'
};

userUtil.setActive = function(userRec, value, doUpdate) {

    var user = scriptIncludeUtil.checkGetGlideRecord(userRec, "sys_user");
    if(user) {
        user.setValue("active", value);
        if(doUpdate)
            user.update();
    }

}
View original source

https://www.servicenow.com/community/developer-articles/utility-for-checking-a-parameter-for-a-method-is-a-valid-record/ta-p/2700521