How to Use ServiceNow Script

How to Use ServiceNow Script Includes Like a Pro

If you’ve ever found yourself writing the same code again and again in ServiceNow, there’s a good chance you need a Script Include. For new developers, Script Includes can seem intimidating, but once you get the hang of them, they’re one of the most powerful tools in your ServiceNow toolkit.

In this blog, we’ll break down Script Includes in a way that makes them easy to understand, show you where they shine, and give you a few tips that can take you from beginner to pro-level usage—without drowning in tech jargon.

How to Use ServiceNow Script

 

What Exactly is a Script Include?

Let’s start simple. A Script Include is basically a reusable chunk of server-side JavaScript code in ServiceNow. Instead of writing the same logic multiple times across your platform, you write it once in a Script Include and then call it wherever you need it. Think of it like a function library in your toolbox.

Here’s a quick analogy: imagine you run a bakery. Every day, you make frosting for your cakes. Would you write the frosting recipe on a new sheet of paper every single day? Of course not. You’d write it once and use it again and again. That’s exactly what a Script Include does—it saves your code recipe for reuse.

Exactly is a Script Include

 

Why Should You Use Script Includes?

If you’re just beginning to get started, you may be asking yourself: why can’t I just write a Business Rule or GlideRecord script wherever I need it?

Here’s why Script Includes are preferable in most situations:

  • Code Reusability: Don’t repeat yourself. Saving time through reusing code eliminates bugs.
  • Centralized Maintenance: Fix a bug once in the Script Include rather than 10 locations.
  • Cleaner Code: Your Business Rules, Workflows, or UI Actions become easier to read.
  • Improved Performance: Especially with GlideAjax (more on this later), Script Includes help reduce client-server round trips.

 

Why Should You Use Script Includes

 

When Should You Use a Script Include?

A few situations where Script Includes are the right fit:

  • You have logic that’s used in multiple Business Rules or Script Actions.
  • You’re performing complex data lookups or calculations.
  • You want to expose server-side logic to client scripts using GlideAjax.
  • You’re building custom APIs or integrations.

If you find yourself copying and pasting code across records, that’s a red flag—it’s probably time to use a Script Include.

Creating Your First Script Include (The Right Way)

Let’s walk through a basic Script Include example.

Example: Get User Manager’s Email

var UserUtils = Class.create();

UserUtils.prototype = {

initialize: function () {},

getManagerEmail: function(userId) {

var user = new GlideRecord(‘sys_user’);

if (user.get(userId)) {

var manager = user.getValue(‘manager’);

var managerRec = new GlideRecord(‘sys_user’);

if (managerRec.get(manager)) {

return managerRec.getValue(’email’);

}

}

return ”;

},

type: ‘UserUtils’

};

How to Use It

In a Business Rule, you can now do this:

var utils = new UserUtils();

var email = utils.getManagerEmail(current.requested_for);

Clean, readable, and reusable.

Using Script Includes with GlideAjax (Client Calls)

Sometimes, you want to access server-side logic from a client-side script (like in a catalog client script). That’s where GlideAjax and Script Includes make a perfect combo.

Here’s a very basic setup:

Script Include (with isPublic)

var CatalogUtils = Class.create();

CatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserEmail: function() {

var userID = gs.getUserID();

var user = new GlideRecord(‘sys_user’);

if (user.get(userID)) {

return user.email;

}

return ”;

}

});

 

Make sure it’s marked as Accessible from: “This application scope only” or All application scopes, and check Client Callable.

Client Script

var ga = new GlideAjax(‘CatalogUtils’);

ga.addParam(‘sysparm_name’, ‘getUserEmail’);

ga.getXMLAnswer(function(response) {

var email = response;

alert(“Your email is: ” + email);

});

This is a game-changer when you need client-side performance but want to keep your business logic secure and centralized.

Best Practices for Script Includes

Want to use Script Includes like a pro? Follow these tips:

1. Use Logical Names

Name your Script Includes clearly. Avoid vague names like Utils123. Use descriptive terms like UserUtils, ApprovalManager, or IncidentHelper.

2. Avoid Hardcoding

Always avoid hardcoding sys_ids or static values. If you must, make them configurable.

3. Keep Functions Small

Break your code into small, single-purpose functions. Easier to maintain and debug.

4. Use gs.info() for Testing

Debugging Script Includes? Add temporary gs.info() lines to print values to the system log.

5. Document Your Code

A comment above each method or key logic block helps you (and others) later. Trust me, you’ll thank yourself in six months.

Real-Life Use Cases of Script Includes

Here are some real examples of how Script Includes make life easier:

  • Custom SLA logic: Centralized rules to evaluate complex conditions.
  • Bulk email sender: A script that loops through users and sends custom messages.
  • Auto assignment logic: Based on multiple business criteria.
  • Time-based triggers: Used in scheduled jobs for reminders, cleanup, or status updates.

Common Mistakes to Avoid

Even experienced developers make some common mistakes. Be mindful of these:

  • Forgetting to return a value – especially in GlideAjax, the result won’t come back to the client.
  • Not making it client-callable – and wondering why your client script isn’t working.
  • Using current in Script Includes – unless passed as a parameter, current doesn’t exist here.
  • Not checking record existence – always check .get() return values before accessing fields.

 

Final Thoughts

Script Includes are one of the most powerful features in the ServiceNow platform. They might seem overwhelming at first, but once you get the hang of them, they can turn your scripts from messy spaghetti into clean, elegant solutions.

Start small. Build a few utility methods. Refactor some repeated code. Before long, you’ll find Script Includes aren’t just a “pro feature”—they’re the secret sauce to scalable, maintainable development in ServiceNow.

 

 

No comment

Leave a Reply

Your email address will not be published. Required fields are marked *