Search This Blog

Friday, January 10, 2020

MAXIMO: Date validation for Classification Attributes

 ****Date validation for Classification Attributes****


Maximo’s Classifications is a powerful tool that allows you to add data field (attributes) for different types of records. It currently supports numbers and string. However there is no out of the box way for it to validate that the user entered a valid date (you end up using string field). This can be a problem if you want to run date range reports against the field or if the field is interfaced with another system. However, there is a way and this is how you can 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 a classification for assets, then the table will ASSETSPEC. Create a Attribute launch point on the “spec table”.ALNVALUE. (e.g. ASSETSPEC.ALNVALUE). Make it for Validation of the field. The language is Jython.
Below is autoscript code



# Script: ALNVALUEDATECHECK
# Date: 12/20/2019
# Author: Prashant Bavane
#
# 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.)
#
# to use this script, first edit this script and for each attribute, add an entry to the table array.
# for example, if you wanted to do the LASTDOT attribute and this
# is the first one you are setting up,
# then set fieldCount=1 and then create table entry records where table[0] = 'LASTDOT'
# You will need to do this for each attribute you want to check. So say you wanted to add another field
# (Aattribute TARGETDATE)
# then increment fieldCount by 1 (now 2) and create table entry records where table[1] is 'TARGETDATE'
# 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/jj/302q would not)
smipleDateFormatPattern = "MM/dd/yyyy" # this must be java's simple date format for the correct date.
#
#
fieldCount = 1 # number for table/classification/attribute to check
table= [ 0 for i in range(fieldCount) ] # array that holds AssetAttrID value.
#
# repeat the following line per attribute to check
# Attribute: INST_DT
table[0] = 'INST_DT' # attribute
#
for i in range(0,fieldCount):
if mbo.getString('ASSETATTRID') == table[i]: # 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]


Before saving, edit the source code and look for the line:table[0] = 'INST_DT' # attribute
Replace INST_DT with the name of your attribute.
Save
Now go test it.
To add another attribute, edit the code
Look for the line: fieldCount = 1 and increment the value by 1 (or the number of attributes you are adding. This value should always
Next, copy the line: table[0] = '….' And increment the number by one, and change the string to the name of your attribute. (E.g. table[1] = 'TARGDATE')
Save
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).

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.

No comments: