Maximo

 View Only
  • 1.  Jython Automation script to stop duplicate asset tag when AssetType is fleet

    Posted 2 days ago

    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
    ------------------------------


  • 2.  RE: Jython Automation script to stop duplicate asset tag when AssetType is fleet

    IBM Champion
    Posted 2 days ago
      |   view attached

    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.io
    https://opqo.io
    ------------------------------

    Attachment(s)

    py
    asset.assettag.unique.py   2 KB 1 version


  • 3.  RE: Jython Automation script to stop duplicate asset tag when AssetType is fleet

    Posted 2 days ago

    You are awesome Jason! This was very timely help. I was thinking of changing whole approach. I will try this solution and get back to you.

    Many Thanks!



    ------------------------------
    Paresh Varatkar
    ------------------------------



  • 4.  RE: Jython Automation script to stop duplicate asset tag when AssetType is fleet

    Posted 2 days ago

    Hi, I did add the script just as you mentioned and added the message via database configuration but script is not firing on duplicate asset tag. Do I need to do DBConfig update for the script changes to take place? I am fairly new to maximo and do not have sharptree configured yet (I will get it eventually) but attached are my script images.



    ------------------------------
    Paresh Varatkar
    ------------------------------