Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

SlightlyLoony
Tera Contributor

Continuing our series of puzzled zerogenarian administrators, today we've got Sally. She's wondering about a facet of scripting on the ServiceNow instance that's had her confused for a while. There are lots of different classes available in the scripting environment. As she would say: "Array and Object and GlideRecord and DBObjectManager and RegExp, oh my!" Where are all these things defined or documented? Why are there different syntaxes used for them?

Sally demands answers...

Well, Sally, I'm sorry to have to break this to you — but it is a big, confusing world out there. And sometimes it's not as friendly as it could (or should) be. But here are some things that might help you make sense of it all.

The classes available to you in JavaScript come in four distinct flavors:

  1. Global JavaScript classes   These are the built-in classes that come with the JavaScript environment. Classes like Object, Array, Math, and RegExp are all examples of these. These classes can be used in scripts on the ServiceNow instance as well as in client scripts. They're universal parts of JavaScript that you can safely assume are always available for use.
  2. Ordinary JavaScript classes   These are classes constructed using standard JavaScript class definitions. You'll find lots of these in the Script Includes on the instance, and in the client scripts that ServiceNow provides. On the ServiceNow instance, any class that's defined in a Script Include is automatically available for your scripts on the ServiceNow instance (but not in your client scripts!). You can add your own classes in Script Includes that you create, and they will be automagically available as well. You can also define your own classes within your own scripts, for use just within that script. Except for the Script Include automagic, all of this is plain old ordinary out-of-the-box JavaScript.
  3. Ordinary Java classes   The ServiceNow instance, as you may already know, is written in the Java programming language. JavaScript has some special features designed to make it easy for Java and JavaScript to interact with each other. Despite their similar-sounding names, Java and JavaScript are actually completely unrelated programming languages, and quite different from one another. For them to work with each other is a bit like asking Nancy Pelosi and John Boehner to share an ice cream soda. It ain't likely to have purty results. But there is a mechanism for Java and JavaScript to play nice together (though not for Nancy and John, apparently). This mechanism uses a special syntax that always starts with the word Packages. If you see this in a script on the ServiceNow instance, then you're seeing this mechanism in action. Note that it won't work in a client script, as there is no Java environment available there. These uses of the Packages feature are intentionally undocumented — they are our careful and deliberate use of an internal Java API that often we really don't want you to emulate. Over the long run, these will all be removed and the Packages feature will be disabled, so any code you write that uses them is at risk of breaking down the road. Best course: don't use the Packages construct at all. As mom would say: do what I say, not what I do.
  4. Scriptable Java classes   These are classes that look in every respect like they're built-in JavaScript classes, but in fact are written in Java — but specially designed to be used from JavaScript. The GlideRecord and GlideElement classes are the most common examples of these. There are a few dozen of them today (and will be many more in future releases, as these will be used to obsolete the current use of Packages). These are classes that we intended for you to use in script, so you should feel perfectly free in doing so. Many of these are documented on the wiki today, and more are being added all the time.


Now that was probably more than Sally really wanted to know, but hopefully she's not quite so puzzled any more...

12 Comments