Including a Java Class in SSJS
You’re most likely aware that SSJS really is really turned into Java under the covers, but did you realize that you can use Java classes in SSJS? In this post, I’ll show how with a simple example.
Using a Java class in SSJS
There are examples similar to this in the DDE help when looking up how to get a document from a view:
var vec = new java.util.Vector(); vec.addElement('firstLookupKey'); vec.addElement('secondLookupKey'); var doc - myView.getDocumentByKey(vec);
And so on. Several view lookup methods take a vector as a way to look up documents or view entries with multiple keys for multiple sorted columns.
The first line specifies that it’s creating a new
java.util.Vector() object. After that, you can use methods of that Java class right from SSJS.
Using Type Ahead Autocompletion for Java Class Methods
If you specify the type of the variable that you create, Domino Designer provide type ahead. Take this example:
var vec:java.util.Vector = new java.util.Vector();
Just type the variable name and a period, and (maybe after a slight pause), you’ll see a list of properties and methods to choose from:
Importing a Java Class into SSJS
If you want to refer to an object more easily in SSJS, you can import the class you want to use and then reference it just by the class name, rather than by the fully-qualified name. To do this, use
Here’s how this would look different when working with a Vector:
importPackage (java.util.Vector); var vec = new Vector();
And now you have code that’s a little easier to follow and also has the ability to display the properties and methods of the class:
Clearly, there is far more opportunity to use this than the simple examples I’ve shown here, but I hope this gets the gears turning and becomes a useful feature for you.