Skip to main content

Four Maximo automation scripts for validating user input
Four Maximo automation scripts for validating user input

·5 min read
Share

These are validation scripts we wrote for Self Service Center offerings in Maximo, but nothing in them is specific to Self Service - they work on almost any field. All four follow the same shape, and understanding that shape matters more than the individual scripts.

Where these go, and what the variables mean

Each one is an attribute launch point: Automation Scripts > Create > Script with Attribute Launch Point, bound to the object and attribute you want to guard, with the Validate event selected.

That launch point gives you two things these scripts depend on:

  • newValue - the value the user just entered, before it is committed.
  • rc and errmsg - the return contract. Set rc to 0 (or False) and put a message in errmsg, and Maximo rejects the input and shows the message. The print statements at the end are how the values are returned.

The print syntax is Python 2 - Maximo runs Jython 2.7, not Python 3. The full list of implicit variables available to a script, and which ones each launch point provides, is in the Maximo automation script documentation.

Validate an IP address

# four parts separated by dots, each a number between 0 and 255
errmsg = ''
rc = 1
ipList = newValue.split('.')

if len(ipList) == 4:
    for i in ipList:
        try:
            i = int(i)
        except:
            rc = 0
            errmsg = i, ' is not a valid number. IP Addresses must contain valid numbers'
            break

        if i > 255:
            rc = 0
            errmsg = i, ' is greater than 255. Valid IP Addresses are between 0 and 255.'
            break
else:
    rc = 0
    errmsg = 'IP Addresses must be in the form nn.nn.nn.nn'

print rc
print errmsg

Note the break in both branches. Without it the loop keeps going and errmsg ends up describing the last bad octet rather than the first, which makes the message confusing when more than one part is wrong.

Check that the end date is after the start date

This one needs a Java import, because Jython has the JVM underneath it:

from java.text import SimpleDateFormat

fmt = SimpleDateFormat('MM/dd/yyyy')
rc = 1
errmsg = ''

# get the fields
startdatestr = offeringAttributes.getValue("STARTDATE")
enddatestr = offeringAttributes.getValue("ENDDATE")

# if both have values, parse them and check the order
if len(startdatestr) > 0 and len(enddatestr) > 0:
    startdate = fmt.parse(startdatestr)
    enddate = fmt.parse(enddatestr)

    if enddate.before(startdate):
        rc = 0
        errmsg = 'End Date must be greater than Start Date'

print rc
print errmsg

The date format is hard-coded to MM/dd/yyyy. If your instance runs a different locale, this silently misparses rather than failing - worth making it match your system setting before you deploy it.

Build a fully qualified hostname from what the user typed

Not a validation, a derivation: take the short name the user entered and construct the FQDN in another field.

domain = offeringAttributes.getValue("HOSTNAME")
server = newValue + '.' + domain + '.abc.com'

offeringAttributes.setNewValue("SERVER", server)

print True

This one was broken in the version we originally published: it assigned the result to a variable called dhostname and then set the field from server, which was never defined. Corrected above. Replace abc.com with your own domain.

Check that a value is numeric

# make sure the value specified is all valid digits
rc = True
errmsg = ''

if newValue.isdigit() == False:
    rc = False
    errmsg = 'Value must be numeric'

print rc
print errmsg

Note this one uses True/False where the first two use 1/0. Maximo accepts both. Pick one and stay with it across your scripts - mixed conventions in a script library are the kind of thing that costs someone twenty minutes at exactly the wrong moment.

A note on indentation

Jython is whitespace-significant, and these scripts lost their indentation when this blog was migrated off its previous platform. They have been restored above. If you are copying automation scripts from anywhere on the web, paste them into a real editor and check the indentation before saving - a script that runs but nests one if wrongly is worse than one that fails outright.

If you have Maximo validation that is not catching what it should, or scripts nobody wants to touch, tell us what you are running.

L

Lubos Strejcek

Content Writer at STREYDA