Build It the Right Way: Making the Most of Script Include and GlideAjax in ServiceNow

BillMartin
Mega Sage

I remember the first time I had to make a client script talk to the server. My catalog item needed to pull the logged-in user’s manager, department, and location in real time. At first, I tried a few things that didn’t work—and then someone mentioned GlideAjax.

 

That moment was a turning point.

 

When You Realize GlideRecord Won’t Help You on the Client Side

 

If you’ve ever tried using GlideRecord directly in a client script, you’ve seen the infamous “undefined” errors or nothing happening at all. That’s because GlideRecord is meant for the server.

So how do you get data from the server to the client in a structured, scalable way?

Enter: Script Include + GlideAjax

 

Understanding the Pattern

The core idea is this: write the logic once on the server using Script Include, and safely call that logic from the client using GlideAjax. Instead of cluttering your client script with logic that doesn’t belong there, you keep things clean, fast, and reusable.

It’s like setting up a reliable communication channel: the client asks a question, the server gives a trusted answer.

 

A Practical Use Case

 

Let’s say you want to retrieve some data about the current user. You don’t want to repeat this logic across every form, widget, or catalog item. Using this pattern, you:

  • Create a Script Include with all the business logic

  • Use GlideAjax in your client-side code to request that logic

  • Handle the response cleanly, often using JSON

What you get is not just a working feature—but a scalable approach that makes your development future-proof.

 

The Aha Moment

 

Once I saw this working—seeing data come from the server in real-time and appear on my form—it all clicked. I’ve since reused this pattern in integrations, automation, approvals, and even in workflows.

And the best part? Debugging becomes easier, performance improves, and you stop repeating yourself.

 

Want to See It in Action?

 

I’ve created a detailed walkthrough where I demo this exact use case—how to use GlideAjax with Script Include the right way.

 

Watch the full tutorial on my YouTube channel 

 

 

This will not only show you the setup but also help you understand when to use it, what to avoid, and how to handle edge cases like returning JSON, managing parameters, and structuring reusable logic.

Let me help you go from frustrated to fluent.

1 ACCEPTED SOLUTION

Thanks so much @JosephW1 for your inputs! You're absolutely right—making the Script Include accessible server-side adds a lot of flexibility, especially for reusable utilities like IRE functions.

 

I have a full comprehensive demonstration about secure code on part 5. Hope this helps 🙂

 

 

View solution in original post

2 REPLIES 2

JosephW1
Tera Guru

Thanks for making the post! GlideAjax is great, isn't it? Here's a tip on how to change lines 4-6 to make your script include also relevant for server-side scripts. I've used this trick to make IRE utilities that can be utilized both client side and server side. Without this trick, your client-side script is rendered useless server-side, which can be a waste of potential.

 

Here's how it looks.

var IncidentDetailsHelperAjax = Class.create();
IncidentDetailsHelperAjax .prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getIncidentDetails: function(incidentNumber) {
        incidentNumber = typeof incidentNumber == 'undefined' ? this.getParameter('sysparm_incidentNumber') : incidentNumber;
        ... //rest of function
    }
});

 

Thanks so much @JosephW1 for your inputs! You're absolutely right—making the Script Include accessible server-side adds a lot of flexibility, especially for reusable utilities like IRE functions.

 

I have a full comprehensive demonstration about secure code on part 5. Hope this helps 🙂