Maximo Constants

January 3, 2022

Introduction

Maximo uses constants as parameters for several common Maximo Business Object (Mbo) methods, such as setting field flags for read only and required behavior or overriding those flags when setting fields or saving a Mbo. I often seen these values provided as a literal number, for example mbo.setValue("STATUS", "APPR", 2), rather than referencing the provided constants. This produces confusing code that does not express the intent of the developer. In this example, what does this magic number 2 mean? The answer is that depending on the context, it may mean "ignore read only flag" or may mean "do not update" or it could mean "do not commit".

In this post we will explore the MboConstants and MaxType classes, so you do not have to use magic numbers any more and can write code that is clear and easy to understand.

Constants

Java Constants

A constant is a variable with a value that does not change after it has been defined. Many languages have a dedicated feature for defining constants, but Java does not. Java does have static fields, that is a field that belongs to the class rather than instances of the class, so the field can be accessed via the class definition. Java also has final, which defines a variable as immutable after it has been defined. So putting the two concepts together provides a globally defined, immutable variable that serves as a constant in Java. For example if I wanted to define the value of Pi as constant it may look like:

public class MathConstants {
public static final double PI = 3.14159265359;
}

This constant can now be referenced as MathConstants.PI;

Note that in Java, by convention constant variables names are upper case with words separated by an underscore _ character.

Twenty years ago when Maximo was first developed it was common to place constants in an interface and then implement that interface wherever those constants were needed. This has come to be considered an anti-pattern, so while Maximo does this, it is best avoided in your own code. A good discussion of this can be found on Stack Overflow, here.

MboConstants

The MboConstants class is the class containing constants related to Mbo operations for Maximo.

We will review the only constants related to setting field values and validation actions. A full listing of the MboConstants values can be found in the Maximo JavaDocs, available for download on the IBM support pages https://www.ibm.com/support/pages/javadocs-maximo-asset-management-76 or hosted on Bruno Portaluri's blog: https://bportaluri.com/wp-content/MaximoJavaDocs76/psdi/mbo/MboConstants.html

Note that the MboConstants class contains a number of "categories" of constants. As mentioned in the introduction, the constants NOACCESSCHECK, NOUPDATE and NOCOMMIT all share the same value, 2. When using constants, use the constant from the appropriate category to ensure clear communication of intent. For example, while mbo.setValue("STATUS", "APPR", MboConstants.NOUPDATE) technically works, it does not communicate the intent of ignore accessing controls and should be expressed as mbo.setValue("STATUS", "APPR", MboConstants.NOACCESSCHECK).

Below are commonly used constants from the MboConstants class, which we will use in our examples.

ConstantDescriptionValue
NOVALIDATIONSuppress validation of a field.1
NOACCESSCHECKSuppress access control (read only) checks.2
NOACTIONUsed to suppress action of a field.3
NOVALIDATION_AND_NOACTIONUsed to suppress validation and action.9
READONLYSets a field as read only.7
REQUIREDSets a field as required.128

Examples

Ignore Read Only

Rewriting the example mentioned in the introduction may look like the following, where it becomes clear we are setting a value that is normally read only.

MboConstants = Java.type("psdi.mbo.MboConstants");
mbo.setValue("STATUS", "APPR", MboConstants.NOACCESSCHECK);

Set Field Required

In this example, the mbo variable is a WORKORDER object and we make the priority a required field.

MboConstants = Java.type("psdi.mbo.MboConstants");
mbo.setFieldFlag("WOPRIORITY", MboConstants.REQUIRED);

Skip Validation Field

MboConstants = Java.type("psdi.mbo.MboConstants");
var mboSet = MXServer.getMXServer().getMboSet("WORKORDER", userInfo);
var mbo = mboSet.add();
mbo.setValue("STATUS", "BOGO", MboConstants.NOVALIDATION);

Skip Validation on Save

MboConstants = Java.type("psdi.mbo.MboConstants");
var mboSet = MXServer.getMXServer().getMboSet("WORKORDER", userInfo);
var mbo = mboSet.add();
mboSet.save(MboConstants.NOVALIDATION);

Multiple Constants

There are cases where you may want to use more than one constant, for example perhaps NOVALIDATION, NOACCESSCHECK and NOACTION. Maximo supports this using the Java pipe | operator for bitwise OR.

MboConstants = Java.type("psdi.mbo.MboConstants");
mbo.setValue("STATUS", "APPR",
MboConstants.NOVALIDATION |
MboConstants.NOACCESSCHECK |
MboConstants.NOACTION
);

MaxType

The MaxType class contains constants that define the integer values that correspond to Maximo data types. This is useful for switch statements when type-specific handling is required.

Below is a table of the MaxType constants.

ConstantDescriptionValue
ALNAlphanumeric text value0
UPPERUppercase text value1
LOWERLowercase text value2
DATEDate3
DATETIMEDate and time4
TIMETime5
INTEGERInteger numeric value (4 bytes)6
SMALLINTSmall integer numeric value (2 bytes)7
FLOATFloating point numeric value8
DECIMALDouble floating point numeric value9
DURATIONNumeric value representing hours10
AMOUNTNumeric value representing currency11
YORNY or N value for boolean values12
GLGeneral ledger formatted value13
LONGALNLong description14
CRYPTOEncrypted value15
CRYPTOXEncrypted value (one way)16
CLOBCharacter large object, large text value17
BLOBBinary large object, large binary value18
BIGINTBig integer numeric value (8 bytes)19

Example

MaxType = Java.type("psdi.mbo.MaxType");
MXSystemException = Java.type("psdi.util.MXSystemException");
function getValue(mbo) {
var dataType = mbo.getMboSetInfo().getMboValueInfo("ATTRIBUTE_NAME").getTypeAsInt();
switch (dataType) {
case MaxType.ALN:
case MaxType.UPPER:
case MaxType.LOWER:
case MaxType.YORN:
case MaxType.CLOB:
case MaxType.LONGALN:
case MaxType.GL:
return mbo.getString("ATTRIBUTE_NAME");
case MaxType.DATE:
case MaxType.DATETIME:
case MaxType.TIME:
return mbo.getDate("ATTRIBUTE_NAME");
case MaxType.INTEGER:
case MaxType.SMALLINT:
case MaxType.BIGINT:
return mbo.getInt("ATTRIBUTE_NAME");
case MaxType.FLOAT:
case MaxType.DECIMAL:
case MaxType.AMOUNT:
case MaxType.DURATION:
return mbo.getDouble("ATTRIBUTE_NAME");
case MaxType.BLOB:
return mbo.getBytes("ATTRIBUTE_NAME");
default:
throw new MXSystemException("system", "invalidType1", Java.to([dataType, parentObject, field], "java.lang.String[]"));
}
}

Conclusion

In this post we reviewed some of the constants provided by Maximo that can be used to create more readable and expressive code. We then provided examples of how these constants can be used to influence Maximo's behavior when performing common actions such as setting and saving values.

Hopefully you have found this useful and if you have any questions or comments please reach out to us at [email protected]

In the time it took you to read this blog post...

You could have deployed Opqo, our game-changing mobile solution for Maximo.

Opqo is simple to acquire, simple to deploy and simple to use, with clear transparent monthly pricing that is flexible to your usage.