This article opens Part 4. Article 7 showed when each save event runs. This one shows what the script’s sets do during that save. We checked IBM’s pages against the behaviour of Manage in the MAS 9.2 series, and we ran the doubtful points on our test system. Where the pages and Manage disagree, this page follows Manage.
One record, one transaction#
Every set in Manage has a transaction. When a set is saved, its transaction saves each set that belongs to it, and then commits once. An error anywhere rolls all of them back.
What decides the transaction is how the script gets the set:
| How the script gets the set | Its transaction |
|---|---|
mbo.getMboSet("ASSET"), through a relationship | the record’s |
mbo.getMboSet("NAME", "OBJECT", "where"), a named set | the record’s |
| a set opened from a record of one of these sets | the record’s |
service.getMboSet("OBJECT", userInfo) | a new one of its own |
We compared the transaction objects on our test system. A related set and a named set had the very
same transaction as their record. A set from service.getMboSet had a different one.
service.getMboSet is a shortcut for the server’s own getMboSet, so both forms behave the same.
In an object or attribute script, never call save#
In an object or attribute launch point, open every set from mbo or from its children, change what
you need, and stop. Manage saves those sets together with the record.
In before save, this costs almost nothing. A value that the script sets on the record goes into the one UPDATE that Manage writes for that record anyway. That statement lists every changed column, and the row’s version stamp guards it.
A set from service.getMboSet breaks this. Manage does not save it with the record, so the script
has to save it, and that save commits on its own. If the record’s save then fails, those changes stay
in the database.
IBM’s pages show how to pull such a set into the record’s transaction, with
mbo.getMXTransaction().add(set). In an object script, the simpler fix is to open the set from mbo
in the first place.
Adding new records: a named set with 1=2#
A relationship returns existing rows. To add new records that belong to the record’s save, open a named set whose where clause matches nothing:
# before save on WORKORDER
logs = mbo.getMboSet("NEWWORKLOG", "WORKLOG", "1=2")
log = logs.add()
log.setValue("description", "Status checked by script")The where clause 1=2 fetches no rows, so the set starts empty. add() creates the new record, and
the save of the work order writes it.
The owner matters here, too. A set opened from mbo knows the work order as its owner, and a work log
takes its record key and class from that owner when it is added. A work log in a set from
service.getMboSet has no owner, and add() fails.
The name is a cache key#
Manage keeps a named set on the record under its name, in upper case. A second call with the same name returns the same set:
- Same name, same where clause: the cached set comes back, with its new records.
- Same name, another where clause: the cached set is reset to the new clause. Its new records are gone, and nothing reports an error.
We saw both on our test system. One new record was in the set. After a second call with another where clause, the same set object held none.
So give each named set a name that says what it is for, and use that name with one where clause only.
After save and after commit: too late for this#
After save runs once the record is written, and after commit once it is committed. To change a record there, the script needs a new set, a new query, the record’s initialization logic again, and a new transaction. That second save runs every save rule again, including the launch point that started it. Variables that the script sets there are not written back either.
Article 7 shows the details. The short rule: work that changes the record or its children belongs in before save.
The script that owns its set#
service.getMboSet is the right call when there is no record to work from: a cron task, a REST
script, or a library called without a record. Then the script owns the transaction, and it should
use it the same way Manage does:
- Open one set with
service.getMboSet. A cron task passesrunAsUserInfo. - Open every other set from the records of that set, as children.
- Save that one set once, at the end. The one save writes the children too.
- Clean up the set in a
finallyblock.
wos = service.getMboSet("WORKORDER", runAsUserInfo)
try:
wos.setWhere("siteid = 'BEDFORD' and wopriority is null")
wo = wos.moveFirst()
while wo is not None:
wo.setValue("wopriority", 3)
wo = wos.moveNext()
wos.save()
finally:
wos.cleanup()Each extra call to service.getMboSet opens another transaction, which commits on its own. Article 26
covers cron task scripts, and article 17 covers the clean-up.
What IBM’s pages leave out#
IBM’s pages warn about save calls in the middle of a transaction, and they explain related sets. They leave out three things:
- The named set’s transaction. IBM’s role script example uses
mbo.getMboSet(name, object, where), but neither page says that this set joins the record’s transaction, or that a1=2clause gives an empty set for new records. - The name trap. Neither page says that a second call with the same name and another where clause drops the new records.
- The owner. Neither page says that some objects, such as work logs, need the owner that only a
set opened from
mbogives them.
Article 33 of this series collects the corrections that matter most.
What comes next#
Article 16 covers the cheapest way to ask whether a row exists.
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.
