The Automation Scripts application asks for the language on the first screen, and the script keeps it for life. IBM’s documents say little about the language itself, beyond a version number that is out of date. So we ran test scripts in both languages on our test system, on the MAS 9.2 series. Every difference on this page was seen there, either when Manage saved a script or when it ran one.
This is the second article of our series on automation scripts. The first article explains what a script is and how Manage runs it.
What ships with Manage#
Manage brings both engines along. You pick one through the language field of the script.
| Jython | JavaScript | |
|---|---|---|
| The language | Python 2.7, running on Java | JavaScript at the ES5 level, running on Java |
| The engine | Jython, shipped with Manage | Nashorn, shipped with Manage |
| Language names Manage accepts | jython, python | javascript, js, nashorn, ecmascript |
| IBM’s examples | nearly all of them | a few |
Both engines compile a script when Manage loads it, and both reach the Java classes of Manage. The difference is in the language you write.
Jython is Python 2.7, without its library#
The Jython in the 9.2 series reports itself as Python 2.7. Three things follow from that, and one surprise.
- Python 3 syntax does not save. We saved a script with an f-string, and Manage refused it with
BMXAA7836E, “The script did not compile”. Use
"{0}".format(x), which works. - Division of two whole numbers gives a whole number.
7/2gives 3 and7/2.0gives 3.5. Convert one side withfloat()when you want the fraction, for example in a cost or a ratio. - Text and numbers do not mix with
+."a" + 1raises a TypeError. Wrap the number instr()first.
The surprise is the library. The Python standard library is not there. import json,
import re and import datetime each fail with an ImportError. IBM’s best-practice page already
says that importing Python modules is not supported, and the test shows how far that goes: even the
modules that come with every Python are missing.
Java fills the gap. Jython imports Java classes like Python modules, and those imports work:
from java.util.regex import Pattern
from java.util import Calendar
from com.ibm.json.java import JSONObject
body = JSONObject()
body.put("wonum", mbo.getString("WONUM"))
responseBody = body.serialize()com.ibm.json.java is the JSON library that IBM’s own script examples use, and it ships with
Manage.
JavaScript is ES5#
The JavaScript engine in Manage runs the language level of ES5. Everything newer either does not save or is missing when the script runs.
- Newer syntax does not save. A script with
letwas refused with the same BMXAA7836E. Arrow functions, template strings, classes, default parameters, destructuring, spread andfor...offail the same way. Writevar,functionand string concatenation. - Newer built-ins are missing.
Map,Set,Promise,Symbol,Object.assign,Array.prototype.includes,Array.prototype.findandString.prototype.startsWithare allundefinedat run time. The script saves, and it fails only on the line that uses one. JSONis there.JSON.parseandJSON.stringifywork, which is the strongest reason to use JavaScript for scripts that read or write JSON.- Never write
"use strict". A script returns its results by assigning variables such asresponseBodythat it never declares. Strict mode forbids exactly that, and the assignment throws a ReferenceError.
The ES5 rule matters most when code comes from somewhere else. A snippet from a current JavaScript
project will almost always use let, an arrow function or a template string, so it fails at save.
Reaching Java from a script#
Both languages call Java, and both need the class first. In Jython that is an import. In
JavaScript, Java.type loads the class.
from psdi.server import MXServer
assets = MXServer.getMXServer().getMboSet("ASSET", mbo.getUserInfo())var MXServer = Java.type("psdi.server.MXServer");
var assets = MXServer.getMXServer().getMboSet("ASSET", mbo.getUserInfo());In JavaScript, MXServer is no global: without the Java.type line it is undefined. Remember to
close a set that you open this way. Article 17 of this series covers why.
Lists behave differently. In Jython, a Python list is converted when a Java method expects a
list, so you can hand one over as it is. In JavaScript, an array stays a JavaScript object, and Java
sees no list. Java.to converts it:
// a Java list
var keys = Java.to(["NAME"], "java.util.List");
// a Java string array
var names = Java.to(["NAME"], "java.lang.String[]");This matters wherever Manage reads a list back from the script. A lookup script, for example, sets
srcKeys and targetKeys, and Manage expects a Java list or a Java string array there. IBM’s
JavaScript example for lookups sets both as plain arrays. Convert them with Java.to.
Which one to pick#
There is no wrong choice, because both languages reach the same objects. Our view, from the differences above:
- Pick Jython for object, attribute, action and condition scripts, and wherever your team keeps existing scripts. Nearly all of IBM’s examples are Jython, so the code in IBM’s pages works as written.
- Pick JavaScript for scripts that read or write JSON, such as REST API scripts and integration
scripts that handle JSON messages.
JSONis built in, and Jython has to use the Java library. - Pick JavaScript also when your scripts are generated from TypeScript. Article 32 of this series shows how that works with the ES5 limit.
Whichever you pick, keep to it inside one group of scripts that call each other. A reader of the code then has one set of rules to keep in mind.
What IBM’s documents get wrong here#
Both IBM documents name Jython 2.7.2, and the scripting guide says that Nashorn comes with the Java runtime. Neither is true for the 9.2 series:
| IBM’s documents | What we found on the MAS 9.2 series | |
|---|---|---|
| Jython | 2.7.2 | 2.7.4 |
| Nashorn | comes with the Java runtime | shipped with Manage, because Java has had no Nashorn since Java 15 |
| JavaScript level | not stated | ES5 |
IBM’s debugging example also adds a decimal number to a text with +, in Jython. It fails with a
TypeError of the same kind as the one in our test. Article 33 of this series collects the corrections that matter
most.
What comes next#
Article 3 of this series shows how scripts, launch points and variables fit together in practice.
IBM’s own pages are the reference to keep next to this series:
- Automation scripts in the IBM Maximo Manage documentation.
- All about Maximo scripting, IBM’s scripting guide on GitHub.
The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.
IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.
