IT CanvassTalk to an advisor
Development · LessonBy , ServiceNow Trainer, 8 yrs · Published · current release · beginner

Script includes

Reusable server-side code you can call anywhere.

Quick answer

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.

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.

Practice challenge

+0 XPStreak ×0
Question 1 of 3
A script include is...

Frequently asked questions

What does the term Script includes refer to in ServiceNow?
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.
What else is worth knowing about Script includes?
The type property must match the name or instantiation breaks.
What is the practical takeaway on Script includes?
Client scripts can't touch the database directly, they call a Script Include that extends AbstractAjaxProcessor via GlideAjax.
What tends to go wrong with Script includes?
Copy-pasting the same logic into many business rules instead of an include. Making includes client-callable unnecessarily. Forgetting the type property, breaking instantiation.
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