I came up with something that seems to work.
Create a relationship:
Parent Object: CLASSSTRUCTURE
Relationshiop: CGCLASSANCESTOR_PARENTTOPLEV
Child Object: CLASSANCESTOR
Where Clause:
classstructureid=:parent
order by hierarchylevels desc
fetch first row only
Remarks:
Relationship to the classancestor table. Used to find the classstructure's *parent* top-level ancestor. The resulting set will contain zero or *one* objects.
Create an automation script:
Object launch point. Object: CLASSSTRUCTURE. Save. Add/Update. Before Save.
errorMsg = """
"You can't create a classification with more than four levels. This is an xyz-specific restriction.
\n Automation script name: CGCLASSSTRUCTUREHIERARCHYPATH."
"""
#Note: CLASSANCESTOR.HIERARCHYLEVELS starts at 0, not 1.
# So if there are "3" HIERARCHYLEVELS, then that means there are actually 4 levels.
if mbo.getInt("CGCLASSANCESTOR_PARENTTOPLEV.HIERARCHYLEVELS") > 2: #Notice that the relationship points to the classstructure's *parent*.
service.error("", errorMsg)
Note: I'm aware that I should make a proper Maximo error message, instead of putting the error message text in the script. This is just a proof-of-concept.