From a3e84af8c56f3c7d15fc3fb05e7c942417c8c578 Mon Sep 17 00:00:00 2001 From: Greg <67873454+1shabby@users.noreply.github.com> Date: Wed, 8 Oct 2025 00:05:32 -0500 Subject: [PATCH] Clean Script Include Framework This is a script include framework that I utilize in my day to day work to help reduce the number of extraneous logs that live on our instances as well as keep the code clean and readable so that new developers on the projects can quickly understand the purpose of the methods and how to utilize them. I've added some mock methods to the script include in code.js and at the bottom of the file I added an example of what it looks like calling the script include from a server script. --- .../Script Includes/Clean Framework/Code.js | 72 +++++++++++++++++++ .../Script Includes/Clean Framework/README.md | 1 + 2 files changed, 73 insertions(+) create mode 100644 Server-Side Components/Script Includes/Clean Framework/Code.js create mode 100644 Server-Side Components/Script Includes/Clean Framework/README.md diff --git a/Server-Side Components/Script Includes/Clean Framework/Code.js b/Server-Side Components/Script Includes/Clean Framework/Code.js new file mode 100644 index 0000000000..2f8fdcd8c9 --- /dev/null +++ b/Server-Side Components/Script Includes/Clean Framework/Code.js @@ -0,0 +1,72 @@ +Script Include Definition: + +var super_utils = Class.create(); +super_utils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, { + initialize: function(debug, noDebugMethods) { + this.debug = false; // Flag to allow debug or not on this script include + this.noDebugMethods = []; // Array of methods to not log from + + if (debug) { + this.debug = debug; + } + + if (noDebugMethods) { + this.noDebugMethods = noDebugMethods.split(','); + } + + // Global Variables For Use In Script + this.CASETYPE1 = '[casetype 1 table]'; + this.CASETYPE2 = '[casetype 2 table]'; + + }, + + /** + * Description: Takes in a method name and message and logs the message in gs.info if debug and method isn't in noDebugMethods + * Parameters: [string] - methodName: name of method calling log. + [string] - msg: message being called in log. + * Returns: None. + */ + + log: function(methodName, msg) { + if (this.debug && this.noDebugMethods.indexOf(methodName) === -1) { + gs.info('[Super_Utils - ' + methodName + '] ' + msg); + } + }, + +/** + * Description: Takes in an input then gets all records where field is input and created by is "iloveearl" + * Parameters: [string] - input: Value that we're querying on. + * Returns: [array][object] - arr: Array of GlideRecord objects matching the conditions. + */ + +superFunction: function(input){ + var arr = []; + var gr = new GlideRecord(this.CASETYPE1); + gr.addQuery('sys_created_by', 'iloveearl'; + gr.addQuery('field',input); + gr.query(); + + while(gr.next()){ + arr.push(gr); + this.log("superfunction", "Created on: " + sys_created_on); + } +return arr; +}, + +/** + * Description: This is a very important method... + * Parameters: None. + * Returns: Logs a very important message. + */ + +superFunction2: function(){ + for (var i = 1; i <= 100000; i++) { + this.log("superFunction2", "ILOVEEARL); + } + } +}, + type: 'super_utils' +}); + +Calling Script Include Via Server Side Script: +var utils = new scope.super_utils('true', 'superFunction2'); \ No newline at end of file diff --git a/Server-Side Components/Script Includes/Clean Framework/README.md b/Server-Side Components/Script Includes/Clean Framework/README.md new file mode 100644 index 0000000000..a626718452 --- /dev/null +++ b/Server-Side Components/Script Includes/Clean Framework/README.md @@ -0,0 +1 @@ +This is a generally useful framework for script includes that in use in my day to day work as well as personal projects on my PDI. It allows you to easily enable logs when calling whichever method you want to use when developing and debugging, then easily disable them by no longer passing in the initilization debug true param or setting it to false in your call. It's very sleek and I highly recommend using it wherever you can to help keep a clean codebase. I also included some examples of how I docment each of my methods. I find it to be helpful when dealing with complex methods I haven't touched in awhile as well as making it easy for new developers to look at the code and hit the ground running quickly. \ No newline at end of file