Attached is a Jython script that validates the asset tag for uniques for assets with a type of FLEET. There is a deploy function that will be called to install the required message entry when you deploy the script from VS Code with the Sharptree VS Code extension:
https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy
This will also create the launch point for you, but otherwise you can see from the scriptConfig that it is an object launchpoint before save on add or update.
I have included the main() function style as I think it is good practice, which I detailed here:
https://www.sharptree.io/blog/2021/2021-11-03-js-functions/
The if 'mbo' in globals()
part is checking to make sure the implicit variable is available, in case you were wondering.
Let me know if you have any questions.
Script pasted below for convenience.
from psdi.server import MXServer
from psdi.mbo import SqlFormat
def main():
# If the script it triggered in the context of an Mbo that is an ASSET with an ASSETTYPE of FLEET and ASSETTAG is not null
if 'mbo' in globals() and mbo.isBasedOn("ASSET") and mbo.getString("ASSETTYPE") == "FLEET" and not mbo.isNull("ASSETTAG"):
# Check if the assettag already exists
assetTag = mbo.getString("ASSETTAG")
try:
assetSet = mbo.getMboSet("$assetSet", "ASSET", "ASSETTAG = :ASSETTAG and SITEID = :SITEID")
if not assetSet.isEmpty():
# If the assettag already exists, throw an error
service.error("asset", "duplicateAssetTag", [mbo.getString("ASSETNUM"), assetTag])
finally:
if assetSet is not None:
assetSet.close()
assetSet.cleanup()
# This function is called when the script is deployed or updated with the Sharptree VS Code extension
# https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy
def deploy():
if 'onDeploy' in globals() and onDeploy == True:
messageSet = MXServer.getMXServer().getMboSet("MAXMESSAGES", userInfo)
try:
sqlf = SqlFormat("msggroup = :1 and msgkey = :2")
sqlf.setObject(1, "MAXMESSAGES", "MSGGROUP", "asset")
sqlf.setObject(2, "MAXMESSAGES", "MSGKEY", "duplicateAssetTag")
messageSet.setWhere(sqlf.format())
if(messageSet.isEmpty()):
message = messageSet.add()
message.setValue("MSGGROUP", "asset")
message.setValue("MSGKEY", "duplicateAssetTag")
message.setValue("DISPLAYMETHOD", "MSGBOX")
message.setValue("MSGIDPREFIX", "BMXZZ")
message.setValue("MSGIDSUFFIX", "E")
message.setValue("VALUE", "Asset {0} already has the asset tag {1}. Please enter a unique asset tag.")
messageSet.save()
finally:
if messageSet is not None:
messageSet.close()
messageSet.cleanup()
main()
scriptConfig="""{
"autoscript": "ASSET.ASSETTAG.UNIQUE",
"description": "Validate that the asset tag is unique",
"version": "",
"active": true,
"logLevel": "ERROR",
"onDeploy": "deploy",
"scriptLaunchPoints": [
{
"launchPointName": "ASSET.ASSETTAG.UNIQUE",
"launchPointType": "OBJECT",
"active": true,
"description": "Validate that the asset tag is unique",
"objectName": "ASSET",
"beforeSave": true,
"save": true,
"add": true,
"update": true
}
]
}"""
------------------------------
Jason VenHuizen
https://sharptree.iohttps://opqo.io------------------------------
Original Message:
Sent: Mon July 01, 2024 12:37 PM
From: Paresh Varatkar
Subject: Jython Automation script to stop duplicate asset tag when AssetType is fleet
There are scripts to avoid duplicate object creation but Is there any way I can disallow duplicate asset tag entry only for certain asset types.
E.g We do not want duplicate assettags for cars. we have a fleet asset tyep. how can I achieve this?
------------------------------
Paresh Varatkar
------------------------------