Script Include – helps you prevent duplicate work and keep your desk clean

General Add comments

Any developer knows the feeling “I think I have done this before, but where is this code?”
In this short article I would like to show some (obvious) advantages of using Script Includes to their fullest.
There are several reasons to move as much server side code as possible to Script Includes.
First of all Script Includes are accessible from any other server side code, and secondly they will only be accessed whenever they are actually needed (which prevents unnecessary checks in Business Rules that will not run anyway).
Recommendation is to set up a script include for every table that has any custom methods.
Depending on the exact setup, you can decide what your initialization should look like:

Name: CustomTableUtilsClass
Description: Class that contains methods that may be used on several occasions
[cc lang=”javascript”]
var CustomTableUtilsClass=Class.create();

// initialize your class.
initialize: function(gr){
// the parameters that you define here, depend on requirement.
// you can pass the GlideRecord,
// or just the sys_id of the current record.
// if the methods have nothing to do with “CustomTable”,
// then it may be wise(r) to define the functions
// in a separate Script Include

// declare the functions that are available for the developer
Functionality1: function(){
var sResult=this._doStuffWith(this.curRecord);
return sResult;

// set up some assisting functions
var sClass=gRecord.getClassDisplayValue();
return sClass;

type: CustomTableUtilsClass

So far pretty straight forward, right?

And this is the idea: keep it simple and keep functions where they should be: as close as possible to their source.
This is why you put the GlideRecord in the initialization, so you don’t have to pass it in every function that you call afterwards.


A bonus that you get whenever you manage to do the initialization with just a GlideRecord and all the functions without parameters (this one is not mandatory, but you’ll have to get dirty to pass the parameters) is this Ajax Script Include:

Name: CustomTableUtilsAjax
Description: Ajax class that accesses the functions in the CustomTableUtilsClass class.
[cc lang=”javascript”]
var CustomTableUtilsAjax = Class.create();

CustomTableUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
executeFunction: function() {
var sysid = this.getParameter(“sysparm_sysid”);
var tbl = this.getParameter(“sysparm_tbl”);
var funnm = this.getParameter(“sysparm_funnm”);

var gr=new GlideRecord(tbl);

var ct=new CustomTableUtilsClass(gr);
var sResult=eval(‘ct.’+funnm+'()’);

return sResult;

With just this Ajax Script Include, you gain access to all the functions that you defined in the CustomTableUtilsClass Script Include.
To call it (example, in a UI Action or similar):

[cc lang=”javascript”]
var ga = new GlideAjax(‘CustomTableUtilsAjax’);
ga.addParam(‘sysparm_name’, ‘executeFunction’);
ga.addParam(‘sysparm_sysid’, g_form.getUniqueValue());
ga.addParam(‘sysparm_tbl’, g_form.getTableName());
ga.addParam(‘sysparm_funnm’, ‘Functionality1’);
var answ=ga.getAnswer();

Please be aware however, that performance may not be optimal here and that some people may get sick when they see the word ‘eval’. sick

You can leave a reply here or send me an email on

Leave a Reply