- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 10-29-2020 06:34 AM
Please note,
I AM NOT A DEVELOPER, BUT JUST WRITING THIS FOR ANY USER TO UNDERSTAND A SCRIPT INCLUDE FROM A NON-DEVELOPER PERSPECTIVE.
The article is based on inputs collected from various sources.
THE SCRIPT INCLUDES:
Script includes help manage the server side scripting more
- Scalable
- Manageable
- Testable.
There are three ways we could use Script includes in:
a) Class Script Includes
b) Classless Script Includes
c) Hybrid Script Includes.
Why Script Includes?
Script includes help centralize the redundant piece of code in one single place and reuse it. It is a repository to store those piece of codes in functions. And obviously, change in script includes directly implies on all those piece of codes that invoke it. In short it is a library of functions.
Demystifying Script Includes:
Class is a blueprint of properties and methods. As we people do not live in blueprint of our house, same thing is true for classes as well. Unless objects are created out of these classes, they simply mean nothing.
Class Script Includes
Creating objects
var obj = new className();
The above piece of code helps create a object out of a class. Few things to keep in mind w.r.t. Classes are:
a) A class can extend another class. Same concept as extending the tables.
b) Extended objects inherit base class properties and methods.
Why is the Script Include Different?
Something that keeps one away from the Script includes is most probably the syntax of it.
'prototype', the curly braces and the semicolon - this is what I am reminded of, when I think of the Script Includes in the first go. But why all these?
Defining classes
Defining the classes happens via two statements in Script includes:
var MyClass = Class.create();
MyClass.protoype = {};
The initialize() function
When an initialize() function is present, code within it will be executed as soon as the Class is instantiated.
The curly braces
Everything between the {and} is in the comma separated format.
name : value, name : value
- Class Script Includes define functions in JSON-like notation.
JSON: JavaScript Object Notation
Simple object: {property1 : value1, property 2 : value2}
ensureArray: function(obj){
var array = new Arry();
if(obj == null || typeof obj == "undefined")
return array;
if(obj.constructor.toString().indexOf("Array) > -1)
return obj;
array.push(obj);
return array;
},
concat: function(parent, children){
for(var i=0; i<children.length; i++){
var item = children[i];
parent.push(item);
}
return parent;
},
See above that the value here is the function itself and that there is a comma after every value.
The keyword 'this'
It is used to reference properties and methods in the current object.
var getUser = Class.Create();
getUser.prototype = {
initialize : function() {
this.userRec = new GlideRecord('sys_user');
this.userRec.get(gs.getUserID());
}
Let's assume that this script include is called from a Business Rule. The business rule is of course defined on a table and it runs on a record in that table. The keyword 'this' in the above context is referring to that record.
NOTE - On the above piece of code, whenever the class is instantiated through the Business Rule, the GlideRecord query gets run and the user record is stored as a global property. This also accounts to more efficiency in code, rather than having to run the GlideRecord query every time you have to execute a function associated with the "getUser" Class.
Private methods:
Private methods in Script includes start with an underscore (_)
This indicates that the function defined in this need not be accessible anywhere outside this script include, even if it is client callable.
_debug : function(str) { //Pass the string to debug log as parameter.
if (this.debug)
gs.print(str);
}
Classless Script Includes:
The Classless Script includes contains on-demand functions, more or less like the global business rules.
- One of the use is that one can use them directly in a Condition clause in ServiceNow. e.g. in UI Actions or Business Rules.
Client Callable checkbox:
Must be set to TRUE for:
- GlideAjax calls from Client scripts.
- List and Report conditions. (Calling the Script include via javascript: keyword)
Do we note this code difference when the checkbox is set to TRUE?
Object.extendsObject(global.AbstractAjaxProcessor, {
//REPLACES
initialize : function() {
What happens here is that a class 'AbstractAjaxProcessor' is used for the client callable Script includes to process the request message.
Hybrid Script Include:
Used as a collection of related functions just like the Classless Script includes.
- Defines a class but not a prototype.
- Does not require a new object to be created. (e.g. - does not need to create objects using the 'new' keyword)
var EmpData = Class.create();
EmpData.getEmpDuration = function() {
//some code here
};
EmpData.getEmpRoles = function (fieldName) {
//Some code here
}
if(EmpData.getEmpDuration != '') {
//display answers
}
Here one can find that the output is readily available with this Script include call. Hence, it is called Hybrid Script include.
Extending Script Includes:
On the above example, if the scope of the Script include you are extending is in a different scope, you can prepend the script include name with the scope name. If a script include is in a global scope the syntax would look like:
MyScriptInclude.prototype = Object.extendsObject(global.NameOfScriptIncludeToExtend, {
From the above said what can also be interpreted about the Client callable Script includes is that for every script include which should be client callable, it should extend the script include 'AbstractAjaxProcessor' which enables the extended script include to be client callable via the code defined in its 'initialize' function.
Expert comment
"You should go for a Script include after you have verified that the piece of code which might be a function does not exist in any baseline configuration (referring to OOTB)."
NOTE - Reference to the baseline configuration does not necessarily call for editing the same, we can very well extend that.
DEVELOPERS MAY EXCUSE BLUNDERS, IF ANY. Comments are most welcome, to keep improving our learning.
Cheers,
Anish
- 9,349 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
It helped me in clearing my doubts about few things related to script includes.
Thanks alot for this content!!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks Amish, glad it helped you.
