The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

Several customers have asked me about the odd-looking "Packages..." invocations they see sometimes in server-side JavaScript (like business rules or script includes). For instance, you might find a piece of code like this:


var a = getCITypes();
JSUtil.logObject(a);

function getCITypes() {
var hs = new Packages.java.util.HashSet();
var gr = new GlideRecord('cmdb_ci');
gr.query();
while (gr.next())
hs.add('' + gr.sys_class_name);
var it = hs.iterator();
var answer = [];
while (it.hasNext())
answer.push('' + it.next());
return answer;
}


This code will return (very inefficiently!) a list of all the types of CIs that are actually in your CMDB. But what the heck is that funny-looking "Packages.java.util.HashSet()" all about?

If you happen to be a Java programmer, you probably already figured out what's going on here: the Packages keyword lets you include Java classes within JavaScript script. If you're not a Java programmer, then nothing I can say in one page is going to make this understandable.

The example I gave above shows the use of a standard Java library class (java.util.HashSet) within a JavaScript script. While this is occasionally useful, the scripts you'll find in your Service-now.com instance generally are accessing various Java classes that are part of the Service-now.com platform. Developers at Service-now.com have access to all the Java code, so it's easy for them to "hook" into various useful little bits here and there. Normally this is not something you'd want to do yourself, as you don't have access to the Java source code and therefore can't look up the classes, methods, and their purposes. However, it may occasionally be useful to copy something you've see us do.

Mozilla has a useful tutorial on Packages>.