service.invokeScript,
which has three forms, and each form passes, returns and fails in its own way. This article shows the
three forms, the flag that one of them needs, and the failures that raise no error at all.The service object lists the three forms. This article explains them. We checked IBM’s pages against the behaviour of Manage in the MAS 9.2 series, and we ran library calls on our test system. Where the pages and Manage disagree, this page follows Manage.
Three ways to call a library#
Any script can be called by name, with or without launch points. A library is simply a script that other scripts call.
| Form | What the library gets | What the caller gets back | Needs the function flag |
|---|---|---|---|
invokeScript(name) | the caller’s record and its usual variables | a map with every variable of the library | no |
invokeScript(name, map) | each map entry as a variable | the same map, filled with every variable of the library | no |
invokeScript(name, function, args) | only the arguments | the return value of the function | yes |
The first two forms run the whole library from top to bottom. The third form calls one function of it.
The map form, as we saw it#
The map form is the classic way to pass data in and get results out:
from java.util import HashMap
ctx = HashMap()
ctx.put("a", 6)
ctx.put("b", 7)
service.invokeScript("CALC", ctx)
r = ctx.get("r")The library CALC holds one line: r = a * b. We ran exactly this shape on our test system, and r
came back as 42.
IBM’s service tables say that this form returns nothing. It returns nothing as a value, but the map
changes. After the call it holds every variable of the library, also those you did not ask for.
In our test, the map also held a helper variable that the library used for its own work, plus
scriptName, service and a few names from the Jython engine.
Two rules follow from that:
- Use a fresh map for each call, and read only the keys you expect.
- Keep library variables tidy. Every name the library sets ends up in the caller’s map.
The caller’s record reaches the library in this form only when the map holds an mbo entry. And the
name is looked up exactly as you write it, so write it exactly as the script is named.
The function form and its flag#
The function form calls one function and returns its result:
# library MATHLIB, with "Allow Invoking Script Functions" ticked
def mult(a, b):
return a * bres = service.invokeScript("MATHLIB", "mult", [2, 3])It needs the flag Allow Invoking Script Functions on the library. IBM’s scripting guide names this step, and the IBM Docs page never mentions it. The flag changes how Manage treats the script:
- The library is loaded once. Manage runs its top-level code when it loads the script, and keeps the result for every later call. If that first run fails, Manage leaves the script out, and only the log says why.
- A function gets only its arguments.
service,mboand the other implicit variables do not exist inside it. Pass in what it needs, for exampleserviceitself. - Module-level variables are shared. A variable at the top of the library keeps its value between calls, for every user and every thread. Use it for constants only.
- A missing function returns
None, with no error.
Without the flag, the function form does not fail. The whole library runs as a plain script, and the
call returns None.
The LIB. prefix sets the flag for you#
A new script whose name starts with LIB., OSIN. or OSOUT. gets the function flag on its first
save. That is convenient for a function library. It is a trap for a library that you call with a map
or with its name only: with the flag set, those two forms no longer copy any variables back. So name
a map-style library without these prefixes.
Failures that raise no error#
Two library failures stay silent, and one does not:
- An inactive library does not run. On our test system, the map call returned normally. The map
gained one entry,
invokeStatusset to false, and the result variables were missing. - A missing function returns
Nonein the function form. - An unknown name raises an error. On our test system: BMXAA7838E, the name is not a valid script name.
So check what comes back. In the map form, check for the keys you expect, or for invokeStatus.
One more setting: mxe.script.jsfuncwrap#
For JavaScript libraries, one system property matters. With mxe.script.jsfuncwrap set to 1, Manage
wraps every JavaScript source in a function when it loads it. Names declared with var, and function
declarations, then stay local to that wrapper. A JavaScript library that hands its functions back
through the map stops working. Leave the property off, or write the library so that it assigns its
results without var. Neither IBM page names this property.
What IBM’s pages get wrong here#
| Topic | IBM’s pages | What Manage does |
|---|---|---|
| The map form | returns nothing | copies every variable of the library back into the map |
| The map’s type | a map of text to text | a map of names to any value; IBM’s own example puts numbers in it |
| The function flag | IBM Docs never names it | the function form needs it |
| The function example | def mult and def add | def add starts with a space, so the library does not compile |
Article 33 of this series collects the corrections that matter most.
What comes next#
Article 14 covers logging from a script without slowing it down.
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.
