Script Includes are reusable server-side code libraries that store business logic. They promote code reusability and maintainability.
Script Includes allow you to centralize logic, avoid code duplication, and create maintainable solutions. They run on-demand, improving performance.
// 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'
};
// 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');
}
// 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
Script Includes that extend AbstractAjaxProcessor can be called from client scripts via GlideAjax.
// 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'
});