What are Script Includes?

Script Includes are reusable server-side code libraries that store business logic. They promote code reusability and maintainability.

Key Benefits

Script Includes allow you to centralize logic, avoid code duplication, and create maintainable solutions. They run on-demand, improving performance.

Creating Script Includes

Basic Script Include Pattern
// Script Include Name: IncidentUtils

var IncidentUtils = Class.create();
IncidentUtils.prototype = {
    initialize: function() {
        // Constructor
    },
    
    // Get high priority incidents
    getHighPriorityIncidents: function() {
        var gr = new GlideRecord('incident');
        gr.addQuery('priority', '<=', '2');
        gr.addQuery('active', true);
        gr.query();
        
        var incidents = [];
        while (gr.next()) {
            incidents.push({
                number: gr.number.toString(),
                priority: gr.priority.toString(),
                description: gr.short_description.toString()
            });
        }
        return incidents;
    },
    
    // Assign incident
    assignIncident: function(incidentSysId, userSysId) {
        var gr = new GlideRecord('incident');
        if (gr.get(incidentSysId)) {
            gr.assigned_to = userSysId;
            gr.state = '2'; // In Progress
            gr.update();
            return true;
        }
        return false;
    },
    
    type: 'IncidentUtils'
};

Calling Script Includes

From Business Rules or Other Scripts
// Create instance and call method
var utils = new IncidentUtils();
var highPriority = utils.getHighPriorityIncidents();

// Assign incident
var success = utils.assignIncident(incidentId, userId);
if (success) {
    gs.info('Incident assigned successfully');
}

Static Methods

Creating and Using Static Methods
// Script Include with static methods
var MathUtils = Class.create();
MathUtils.prototype = {
    type: 'MathUtils'
};

// Static methods (don't require instantiation)
MathUtils.calculatePercentage = function(value, total) {
    if (total == 0) return 0;
    return (value / total) * 100;
};

MathUtils.roundToTwo = function(num) {
    return Math.round(num * 100) / 100;
};

// Usage - No need to create instance
var percent = MathUtils.calculatePercentage(45, 100); // 45
var rounded = MathUtils.roundToTwo(3.14159); // 3.14

Client Callable Script Includes

Script Includes that extend AbstractAjaxProcessor can be called from client scripts via GlideAjax.

Client Callable Script Include
// Script Include: ClientUtils
// Client callable: TRUE

var ClientUtils = Class.create();
ClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getUserCount: function() {
        var groupId = this.getParameter('sysparam_group_id');
        
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', groupId);
        gr.query();
        
        return gr.getRowCount().toString();
    },
    
    type: 'ClientUtils'
});

CSA & CAD Exam Questions (13 Questions)

Where do Script Includes execute? Q1
CSA
  • A) In the browser
  • B) On the server
  • C) In both locations
  • D) In the database
Show Answer
Correct Answer: B
Script Includes execute on the server only. They are server-side reusable code libraries.
What is the primary benefit of using Script Includes? Q2
CSA
  • A) Faster execution
  • B) Code reusability and maintainability
  • C) Better security
  • D) Automatic backups
Show Answer
Correct Answer: B
Script Includes promote code reusability and maintainability by centralizing common logic in one place.
How do you create an instance of a Script Include? Q3
CSA
  • A) ScriptInclude.create('Name')
  • B) var obj = new ScriptIncludeName()
  • C) var obj = ScriptIncludeName.getInstance()
  • D) var obj = gs.include('ScriptIncludeName')
Show Answer
Correct Answer: B
Create instances using: var obj = new ScriptIncludeName(); then call methods: obj.methodName();
Can Script Includes use GlideRecord? Q4
CSA
  • A) No, only in Business Rules
  • B) Yes, they run server-side
  • C) Only if marked "Client callable"
  • D) Only in global scope
Show Answer
Correct Answer: B
Yes! Script Includes run server-side and have full access to GlideRecord, gs, and all server APIs.
What must a Script Include extend to be callable from client scripts? Q5
CAD
  • A) ClientCallable
  • B) AbstractAjaxProcessor
  • C) GlideAjax
  • D) BaseClass
Show Answer
Correct Answer: B
Script Includes must extend AbstractAjaxProcessor and be marked "Client callable" to be called from client scripts via GlideAjax.
Do Script Includes require instantiation to call static methods? Q6
CAD
  • A) Yes, always
  • B) No, static methods are called directly
  • C) Only for admin users
  • D) Only in scoped applications
Show Answer
Correct Answer: B
Static methods can be called directly without creating an instance: ClassName.staticMethod(); Regular methods require: var obj = new ClassName(); obj.method();
Where are Script Includes stored? Q7
CSA
  • A) sys_script table
  • B) sys_script_include table
  • C) sys_ui_script table
  • D) sys_script_client table
Show Answer
Correct Answer: B
Script Includes are stored in the sys_script_include table. Navigate to System Definition > Script Includes.
Can one Script Include call another? Q8
CAD
  • A) No, not allowed
  • B) Yes, create instance and call methods
  • C) Only static methods
  • D) Only with special permissions
Show Answer
Correct Answer: B
Yes, Script Includes can call other Script Includes. Just create an instance and call the methods as normal.
What is the purpose of the initialize() function? Q9
CAD
  • A) Required by ServiceNow
  • B) Constructor called when instance is created
  • C) Initializes the database
  • D) Loads dependencies
Show Answer
Correct Answer: B
initialize() is the constructor method that runs when you create a new instance: var obj = new ClassName(); Use it to set up initial state.
When are Script Includes loaded? Q10
CAD
  • A) On system startup
  • B) On-demand when called
  • C) On every page load
  • D) During scheduled maintenance
Show Answer
Correct Answer: B
Script Includes are loaded on-demand when they're called, improving performance by not loading unnecessary code.
Can Business Rules call Script Includes? Q11
CSA
  • A) No, different execution contexts
  • B) Yes, both are server-side
  • C) Only async business rules
  • D) Only before business rules
Show Answer
Correct Answer: B
Yes! Business Rules commonly call Script Includes to centralize business logic. Both run server-side.
What does the 'type' property do in a Script Include? Q12
CAD
  • A) Defines data type returned
  • B) Identifies the class name
  • C) Sets the script type
  • D) Optional metadata only
Show Answer
Correct Answer: B
The type property identifies the class name and is convention in ServiceNow Script Includes: type: 'ClassName'
Are Script Includes table-specific? Q13
CSA
  • A) Yes, like Business Rules
  • B) No, they're reusable across all tables
  • C) Only if configured that way
  • D) Depends on the scope
Show Answer
Correct Answer: B
Script Includes are NOT table-specific. They're reusable code libraries that can work with any table or be used for general utilities.

Key Points

  • Server-side reusable code
  • Promote code reusability
  • Create with Class.create()
  • Instance: new ClassName()
  • Static: ClassName.method()
  • Ajax: extend AbstractAjaxProcessor
  • Loaded on-demand