Skip to content
IT Canvass
Development · Lesson

Script includes

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.

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. A script include is...

    • A. Reusable server-side code
    • B. A stylesheet
    • C. A report
    Show answer

    A. Reusable server-side code

    It centralises logic for reuse.

  2. To call one from client code, tick...

    • A. Client callable
    • B. Read only
    • C. Active only
    Show answer

    A. Client callable

    Client callable exposes it to GlideAjax.

  3. Script includes help you avoid...

    • A. Copying logic everywhere
    • B. Using tables
    • C. Logging in
    Show answer

    A. Copying logic everywhere

    They keep one authoritative copy of the code.

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.
CallWhatsAppEnquire