Date Validation for Classification Attributes
Maximo’s Classifications is a powerful tool that allows you to add data fields (attributes) for different types of records. This Classifications tool currently supports numbers and strings. However, there is no out-of-the box functionality for it to validate that the user entered a valid date. In that case, you end up using string fields. This can pose a problem if you want to run date range reports against the field or if the field is interfaced with another system. Nevertheless, there is still a way to make the user enter a valid date in a classification attribute.
This approach does make a few assumptions:
- An attribute that is to be used for a date will always be used for a date in all classification where it is used. For example, if you create an attribute called INST_DT, then it is assumed it will be a date for any classification that used the INST_DT attribute.
- This approach only supports one date format (currently MM/DD/YYYY, but this can be changed to a different format). A date not entered in that format will generate an error, even if it is a valid date.
- Adding an attribute does require the modification of the autoscript (see below).
So how to use this.
- First, create your attribute that is to be a date field.
- Next, create a new autoscript.
- The launch point will be the “spec” table where the attribute is to be used. For example, if you are using classification for assets, then the table will ASSETSPEC. Create an Attribute launch point on the “spec table”.ALNVALUE. (e.g. ASSETSPEC.ALNVALUE). Make it for Validation of the field. The language is Jython. Use the attached file as the source code.
- Before saving, edit the source code and look for the line:
attributes.append('INST_DT')
Replace INST_DT with the name of your attribute. - Save.
- Now go test it.
- To add another attribute, edit the code.
- Next, copy the line:
attributes.append('….')
And change the string to the name of your attribute. (E.g.attributes.append('TARGDATE')
). - Save.
- Next, copy the line:
- To use this on a new spec table (like locationspec, ticketspec, workorderspec, etc), create a new launch point on the “spec table”.alnvalue like you did in the first one. You only need to have one launch point for table (not one per attribute).
I tried to explain all this in the autoscript so if you implement and down the road you want to add a new attribute, you can look at the autoscript and not have to search your email on how to do it.
Script Code:
# Script: ALNVALUEDATECHECK
# Date: 01/12/2016
# Author: Mike Chrisman
#
# Description: this script will validate that a user has entered a valid date
# for the ALNVALUE field on the ...SPEC Field (EG. ASSETSPEC, LOCATIONSPEC, Etc.)
#
# Modification: 01/10/2020: Changed so it just looks at the attributename instead of Classication/Table/attribute.
# Cleaned up code
#
#
# To use this script, first edit this script and for each attribute, add an entry to the attributes array.
# for example, if you wanted to do the LASTDOT attribute and this
# is the first one you are setting up,
# then edit the line attributes.append('LASTDOT') and change LASTDOT to the name of your attribute
# You will need to do this for each attribute you want to check. So say you wanted to add another field
# (attribute TARGETDATE) then copy the line attributes.append('...') and change it to the name of you attribute
# Repeat for each time you want to add a field to be validated as a date.
#
# Once you have added the attribute to this script, you will then need to create a launchpoint
# on the table.alnvalue attribute/Validate action. (E.G. Assetspec.ALNVALUE field). You will only
# need to create one lauch point per table (ASSETSPEC, LOCATIONSPEC, etc.) This code is Jython script.
#
# Once an attribute has been added to the script (and a launch point created for the correct 'spec' table),
# then every time that attribute is used on any classification on that table, this script will validate that it
# is a date.
#
from java.text import SimpleDateFormat
from java.util.regex import Matcher
from java.util.regex import Pattern
#
# this script currently only supports the date format: MM/DD/YYYY
# this system check that first the entry is in the correct format and then that it is a valid date.
# for some reason SimpleDateFormat is not restrictive so we have to do this so things like 11/30/99j do not pass
# (which SimpleDateFormat says is okay)
# To change to a different format, modify the following lines to fit your needs
regexPattern = "\\d{2}/\\d{2}/\\d{4}" # this must insure that it is in the correct format, not a valid date
# (so 99/82/1000 would pass this check but 01/03/302q would not)
smipleDateFormatPattern = "MM/dd/yyyy" # this must be java's simple date format for the correct date.
#
#
attributes = []
#
# Create a copy of the following line for each attribute you are adding.
attributes.append('INST_DT')
#
#
for attribName in attributes:
if mbo.getString('ASSETATTRID') == attribName: # check to see if we are on the correct attribute
valueToCheck = mbo.getString("ALNVALUE")
if len(valueToCheck) > 0: # we let blank fields pass
# first step is to make sure the field is in the correct format. We use regex for this
regPattern = Pattern.compile(regexPattern)
matcher = regPattern.matcher(valueToCheck)
if matcher.matches():
# we use the Java SimpleDateformat and try to parse it. If it fails then we
# assume it is in an invalid date.
try:
format = SimpleDateFormat(smipleDateFormatPattern)
format.setLenient(False)
date = format.parse(valueToCheck)
except:
errorgroup = "configure"
errorkey = "BlankMsg"
params = ["Invalid date format. The value must be in the format: " + smipleDateFormatPattern]
else:
errorgroup = "configure"
errorkey = "BlankMsg"
params = ["Invalid date format. The value must be in the format: " + smipleDateFormatPattern]