<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>post Utility for checking a parameter for a Method is a valid record for a table. in Developer articles</title>
    <link>https://www.servicenow.com/community/developer-articles/utility-for-checking-a-parameter-for-a-method-is-a-valid-record/ta-p/2700521</link>
    <description>&lt;P&gt;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.&amp;nbsp; 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.&amp;nbsp; 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.&amp;nbsp; I thought I would share it for others to use if they so chose.&amp;nbsp; Since it can make your Script Include more flexible in how you use them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is below but I also attached an XML file that you can just import.&amp;nbsp; There is also a simple example at the bottom.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;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 &amp;amp;&amp;amp; record instanceof GlideRecord &amp;amp;&amp;amp; record.getTableName() == table){
		return record;
	} else {
		return null;
	}
	
};

scriptIncludeUtil.checkString = function(record){
	
	if(record &amp;amp;&amp;amp; (typeof record == "string" || record instanceof String)){
		return true;
	} else {
		return false;
	}
	
};
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Example:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;//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();
	}

}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Oct 2023 17:22:57 GMT</pubDate>
    <dc:creator>DrewW</dc:creator>
    <dc:date>2023-10-16T17:22:57Z</dc:date>
    <item>
      <title>Utility for checking a parameter for a Method is a valid record for a table.</title>
      <link>https://www.servicenow.com/community/developer-articles/utility-for-checking-a-parameter-for-a-method-is-a-valid-record/ta-p/2700521</link>
      <description>&lt;P&gt;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.&amp;nbsp; 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.&amp;nbsp; 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.&amp;nbsp; I thought I would share it for others to use if they so chose.&amp;nbsp; Since it can make your Script Include more flexible in how you use them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is below but I also attached an XML file that you can just import.&amp;nbsp; There is also a simple example at the bottom.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;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 &amp;amp;&amp;amp; record instanceof GlideRecord &amp;amp;&amp;amp; record.getTableName() == table){
		return record;
	} else {
		return null;
	}
	
};

scriptIncludeUtil.checkString = function(record){
	
	if(record &amp;amp;&amp;amp; (typeof record == "string" || record instanceof String)){
		return true;
	} else {
		return false;
	}
	
};
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Example:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;//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();
	}

}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 17:22:57 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-articles/utility-for-checking-a-parameter-for-a-method-is-a-valid-record/ta-p/2700521</guid>
      <dc:creator>DrewW</dc:creator>
      <dc:date>2023-10-16T17:22:57Z</dc:date>
    </item>
  </channel>
</rss>

