IT CanvassTalk to an advisor
Development · LessonReviewed by Sneha I, ServiceNow Trainer, 8 yrs · Updated · Published · current release · beginner

ServiceNow Script includes

Reusable server-side code you can call anywhere.

Quick answer

A Script Include is reusable server-side code in ServiceNow, defined with Class.create and a prototype whose type property matches the class name, and called from business rules, flows or other scripts. To reach it from a form, a client script calls a client-callable include that extends AbstractAjaxProcessor through GlideAjax, asynchronously, never with getXMLWait.

Key takeaways
  • The class pattern
  • Calling from the client: GlideAjax
  • On-demand functions and scopes
  • Common mistakes
  • Try it Yourself

Script Includes are reusable server-side code, the place to keep logic you call from multiple business rules, flows, or client scripts (via GlideAjax). They are ServiceNow's answer to "don't repeat yourself", and the correct home for anything non-trivial.

The class pattern

var IncidentUtils = Class.create(); IncidentUtils.prototype = { initialize: function() {}, isVip: function(userId) { var u = new GlideRecord('sys_user'); return u.get(userId) && u.vip == true; }, type: 'IncidentUtils' // required, the class name };

Call it anywhere server-side: new IncidentUtils().isVip(gs.getUserID()). The type property must match the name or instantiation breaks.

Calling from the client: GlideAjax

Client scripts can't touch the database directly, they call a Script Include that extends AbstractAjaxProcessor via GlideAjax. This is the correct, non-blocking way to fetch server data on a form:

// SERVER: client-callable Script Include var IncidentAjax = Class.create(); IncidentAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { getVip: function() { var u = new GlideRecord('sys_user'); return (u.get(this.getParameter('sysparm_user')) && u.vip) ? 'true' : 'false'; }, type: 'IncidentAjax' }); // CLIENT: async call, note the callback var ga = new GlideAjax('IncidentAjax'); ga.addParam('sysparm_name', 'getVip'); ga.addParam('sysparm_user', g_form.getValue('caller_id')); ga.getXMLAnswer(function(ans) { if (ans == 'true') g_form.addInfoMessage('VIP caller'); });

On-demand functions and scopes

Mark a Script Include Client callable only when it is actually used by GlideAjax, each client-callable include is a potential data-exposure surface. In a scoped app, set the correct accessible from scope so other scopes can (or cannot) call it.

Common mistakes

  • Copy-pasting the same logic into many business rules instead of an include.
  • Making includes client-callable unnecessarily.
  • Forgetting the type property, breaking instantiation.
  • Using the synchronous getXMLWait() form and freezing the browser.
Try it YourselfJavaScript
▸ Press Run to execute.

Runs in a sandbox in your browser. Mock gs and GlideRecord and sample data are provided.

Practise this on your own free instance.
Set up your free instance →
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support