↓ Skip to main content
An ink and watercolour drawing of an open book on a wooden reading stand in the middle of a workbench, its pages showing wavy lines. A teal crank machine stands on the left and a teal machine with an orange funnel on the right, and a thin rose string runs from each machine to the book's ribbon bookmark.

Library Scripts and Function Calls

Maximo Automation Scripts - This article is part of a series.
Part 13: This Article
A library script holds code that many scripts share. The call goes through 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.

FormWhat the library getsWhat the caller gets backNeeds the function flag
invokeScript(name)the caller’s record and its usual variablesa map with every variable of the libraryno
invokeScript(name, map)each map entry as a variablethe same map, filled with every variable of the libraryno
invokeScript(name, function, args)only the argumentsthe return value of the functionyes

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 * b
res = 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, mbo and the other implicit variables do not exist inside it. Pass in what it needs, for example service itself.
  • 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.

Which form of invokeScript to use Which form of invokeScript to use
Which form of invokeScript to use

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, invokeStatus set to false, and the result variables were missing.
  • A missing function returns None in 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
#

TopicIBM’s pagesWhat Manage does
The map formreturns nothingcopies every variable of the library back into the map
The map’s typea map of text to texta map of names to any value; IBM’s own example puts numbers in it
The function flagIBM Docs never names itthe function form needs it
The function exampledef mult and def adddef 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:

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.

Maximo Automation Scripts - This article is part of a series.
Part 13: This Article