↓ Skip to main content
An ink and watercolour drawing of one letter in three stages on a wooden workbench: on the left an open sheet with wavy lines and a fountain pen on it, in the middle the letter in an envelope closed with a red wax seal, and on the right a teal mailbox with its red flag raised and the envelope in its slot.

Before Save, After Save, After Commit: Picking the Moment

Maximo Automation Scripts - This article is part of a series.
Part 7: This Article
A save launch point asks one question when you create it: before save, after save or after commit. The answer decides what your script can change, whether its errors reach the user, and whether a failure can be undone. This article shows what each moment gives you and what it takes away.

The previous article showed the order of one save. This one is about the choice that a save launch point asks for. We checked IBM’s pages against the behaviour of Manage in the MAS 9.2 series, and where the two disagree, this page follows Manage.

The three moments side by side
#

Before saveAfter saveAfter commit
Has the SQL run?noyesyes
Is the data committed?nonoyes
Values the script sets on mbogo into the same UPDATEneed a second saveneed a second save
OUT and INOUT variableswritten backignoredignored
An error in the scriptstops the save, rolls backrolls back the saveis only logged
Does the user see the error?yesyesno

The rest of this article follows from this table.

Before save: the place for business logic
#

Before save is the moment for logic that changes the record or its children. The script runs inside the save, before the SQL. A value it sets on mbo simply becomes part of the UPDATE that Manage writes for that record anyway. There is one UPDATE per record, and it carries every changed column.

Related records work the same way. A set that you open from mbo through one of its relationships belongs to the same transaction. Manage saves it together with the record. Do not call save() on it yourself.

For checks, there is an even earlier moment: the validate application event. It runs at the start of the save, before any save logic has changed the record. Article 6 shows its place in the order.

After save: inside the transaction, too late for the record
#

After save runs once the database has executed the SQL, and before the commit. The data is written but not final. If the script raises an error, the whole transaction is rolled back.

That makes after save right for one kind of work: something outside the record that must be undone together with it. IBM’s own pages name writes to external storage as the example.

It also makes after save the wrong place for business logic:

  • Variables are not written back. An OUT or INOUT variable that the script sets after save is silently ignored.
  • Changing the record means saving it again. The script has to fetch the record in a new set, with a new query and the record’s initialization logic running again, and save it in a new transaction.
  • A second save runs every save rule again. That includes this launch point. A script that changes a value on every run can loop.
  • Half of the work can commit. A set fetched through service.getMboSet has a transaction of its own. Its changes can commit while the record itself rolls back.

After commit: for what must wait until the data is final
#

After commit runs once the data is committed. Nothing can be undone now. That fits work that must only happen when the change is certain, such as a notification when an asset is reported broken, or a message to another system when a work order is completed.

One fact decides how you write such a script: an error after commit never reaches the user. Manage writes it to the log and continues. The user sees a successful save. So log clearly, and never use after commit for a check that should stop anything.

Variables are not written back here either.

Four questions that pick the moment
#

Four questions that pick the moment of a save script Four questions that pick the moment of a save script
Four questions that pick the moment of a save script

Most scripts end at the second question. Before save is the default, and the other two moments are for work that is outside the record.

What IBM’s pages leave out
#

IBM’s pages describe the three moments with good examples. They leave out the costs:

  • Neither page says that after save and after commit ignore OUT and INOUT variables.
  • Neither page says that an error after commit is only logged, so the user never sees it.

Article 15 of this series goes deeper into transactions: which sets join the record’s transaction, and how to add new records to it.

What comes next
#

Article 8 moves from objects to fields: attribute launch points and their events.

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 7: This Article