Hi Ryan: my apologies for not posting the final solution. Actually I ended up doing 3 different scripts, because I had to cover 3 different events. It was fairly tricky to do, but I'm confident I have the solution (it's been tested but not implemented yet due to other priorities).
Here's the final version of the first script. As you can see, there's a reference to another script running on the Save event.
##########################################################################################################
# Author: Shannon Rotz
# Purpose : When a meter reading is entered into the WORKORDER.METERREADING field (user-defined), validate the reading.
# The reading will be added to the asset's meter history when the work order is saved.
# Creation Date : 2022-01-28
# Update Date :
##########################################################################################################
from psdi.util.logging import MXLogger
from psdi.util.logging import MXLoggerFactory
from psdi.mbo import MboConstants
from java.util import Date
from java.util import Calendar
from psdi.server import MXServer
logger = MXLoggerFactory.getLogger("maximo.script")
logger.debug("==================== WO-METERREADING." + launchPoint + ": START =====================")
#17
def getHourDiff(timeDiff):
milliseconds = 1000
milliminutes = milliseconds * 60
millihours = milliminutes * 60
millidays = millihours * 24
daysDiff = timeDiff / millidays
hoursDiff = timeDiff/millihours
minDiff = timeDiff / milliminutes
#if(hoursDiff != 0):
return int(hoursDiff)
#30
woReading = mbo.getString("NEWREADING")
asset = mbo.getString("ASSETNUM")
logger.debug("Meter Reading: " + str(woReading ) )
#36
if woReading != None and woReading != '' and asset != None and asset != '':
assetMeterSet = mbo.getMboSet("PRIMARYMETER")
try:
if assetMeterSet.isEmpty() or assetMeterSet.count() != 1:
errorgroup = 'asset'
errorkey = 'noprimary'
#44
else:
assetMeter = assetMeterSet.getMbo(0)
logger.debug("Active meter found.")
lastReading = assetMeter.getString("LASTREADING")
lastReading = lastReading.replace(',','')
lastReadingDate = assetMeter.getDate("LASTREADINGDATE")
mUnit = assetMeter.getString("METER.MEASUREUNITID")
logger.debug("Last reading was " + lastReading + " on " + str(lastReadingDate) + ".")
logger.debug("Unit of Measure is " + mUnit + ".")
if float(lastReading) > float(woReading):
errorgroup = 'meter'
errorkey = 'resetNotAllowed'
#57
else:
if mUnit == "HOURS" or mUnit == "HOUR":
cal=Calendar.getInstance()
currentDate = MXServer.getMXServer().getDate()
timeDiff = currentDate.getTime() - lastReadingDate.getTime()
hoursElapsed = getHourDiff(timeDiff.longValue())
logger.debug("Hours since last reading are " + str(hoursElapsed))
maxValue = float(lastReading) + hoursElapsed
if float(woReading) > maxValue:
errorgroup = 'meter'
errorkey = 'invalidReading'
#70
finally:
# Make sure you close your sets to avoid leaks.
assetMeterSet.close()
logger.debug("==================== WO-METERREADING." + launchPoint + ": END =====================")