↓ Skip to main content
An ink and watercolour drawing of an open wooden file drawer with a brass handle and a blank label holder on a workbench. A row of orange and teal folders stands inside, and a hand lowers one more folder, with a small blank paper tag tied to its corner, into the drawer.

Attachment Store Scripts

Maximo Automation Scripts - This article is part of a series.
Part 28: This Article
Manage keeps attachments through one store, chosen by a system property: a folder, an object store, or a script. As the store, a script decides where every attached file goes and where it comes back from. This article shows how to set one up, what each of its functions receives, and two pieces of IBM’s advice that Manage contradicts.

This article closes Part 7. We checked IBM’s attachment store page against the behaviour of Manage in the MAS 9.2 series, and we checked the method names on our test system. Where the page and Manage disagree, this page follows Manage.

How a script becomes the store
#

The system property mxe.attachmentstorage names the store. Its value is either a Java class or script:<NAME>:

mxe.attachmentstorage=script:MYSTORE
  • The name is case-sensitive. Write it as the script is stored, in upper case.
  • The script needs “Allow Invoking Script Functions”. Without it, every call runs the whole script body and calls no function.
  • An empty property falls back to mxe.oslc.attachmentstorage, and when that is empty too, to the file store on disk.

The four functions
#

When the store script's functions run When the store script's functions run
When the store script’s functions run

A function that the script does not define is skipped without an error. Without createAttachment or deleteAttachment, nothing happens in the store. Without getAttachment, no data comes back.

FunctionWhat ctx holdsWhat the script does
hasAttachmentthe new name; the attachment record, or None for mailchecks whether the name is taken
createAttachmentthe name, the bytes and the MIME type; no attachment recordwrites the bytes
getAttachmentthe attachment record, or only the URL namereturns the bytes with ctx.setAttachmentData(...)
deleteAttachmentthe attachment record onlyremoves the file

The ctx object also offers every method of service, so ctx.error(...) raises an error from the store.

hasAttachment: the name
#

Before Manage calls hasAttachment, it has already checked the name: no invalid characters, no trailing dot, and an allowed file extension from mxe.doclink.doctypes.allowedFileExtensions. The script only has to check for a duplicate.

The name comes from ctx.getFileName(ctx.getAttachmentName()). The record from ctx.getMbo() can be None, because the mail listener attaches files without one.

For a duplicate, the script has two choices:

  • Give a new name with ctx.setOverrideFileName("report-2.pdf"). Pass the plain name.
  • Report the duplicate with ctx.setHasAttachment(True) and no new name. Manage then adds a hyphen and a time stamp in milliseconds before the extension.

Leave the top-level path to Manage. IBM’s page tells the script to put the value of mxe.doclink.doctypes.topLevelPaths in front of the new name. Manage adds that prefix itself, in front of the final name. A script that adds it as well gets it twice. IBM’s own example on the same page leaves it to Manage, which is right.

An override name always wins in the 9.2 series. Calling setHasAttachment(False) first is not needed for it.

deleteAttachment: the name comes from the record
#

IBM’s page says that deleteAttachment finds the file name with the same methods as createAttachment. It cannot. For a delete, Manage passes only the attachment record: ctx.getAttachmentName() returns None, and ctx.getFileName(None) fails with a NullPointerException. Read the name from the record:

def deleteAttachment(ctx):
    name = ctx.getMbo().getString("urlname")
    remove_file(name)

remove_file stands for your own code. The example script further down IBM’s page does the same.

Two more details
#

  • The MIME type setter is spelled setAttachmenMimeType, without the t in “Attachment”. On our test system, the correct spelling was missing. The getter is getAttachmentMimeType.
  • Mail attachments can stream from the store. A script variable datasource with the value 1 makes Manage read a mail attachment through getAttachment when the mail needs its content.

Scripts that are not the store
#

Every other script that writes or reads attachments should go through the store Manage has configured. A script that writes into the attachment folder on disk works only while the file store is in use. When the store moves to object storage or to a script, the script writes to a place nobody reads.

What IBM’s page gets wrong here
#

TopicIBM’s pageWhat Manage does
The name in deleteAttachmentthe same methods as in createAttachmentthe name is None there; read urlname from the record
The prefix for a duplicatethe script adds topLevelPathsManage adds it; adding it again doubles it
setHasAttachment(False) before an overriderequired in the 9.1 seriesan override wins in the 9.2 series either way
The function setfour functionsfour, and a missing one is skipped without an error

Article 33 of this series collects the corrections that matter most.

What comes next
#

This ends Part 7. Part 8 is about development practice: naming and governing a script estate, testing, debugging with the warnings framework, and writing scripts in TypeScript.

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