Creating automation script to create parent classification in Maximo IT

About this task

An automation script is created for creating parent classification in Maximo IT for the Claroty devices.

Follow the steps below to create the automation script:

Procedure

  1. Open the Automation Scripts application.
  2. On the navigation panel, click Create and select Script with Action Launch Point : Step 1 of 3, and enter data in the fields as follows:
    • Launch Point: Specify the launch point name, for example, CLAROTYBASECLASS.
    • Object: Search for the ASSET object and select it.
    • Action: Specify the Action name same as the name given for Launch Point, for example, CLAROTYBASECLASS.
    • Check the checkbox for Active? and click Next.
  3. On the Create Script with Action Launch Point : Step 2 of 3 pop-up, enter the details in the fields as follows:
    • Script: Specify as Jython
    • Script Language: Search and select jython
    • Log level: Search and select ERROR
    • Click Next.
  4. On the Create Script with Action Launch Point : Step 3 of 3. In the Script box, enter the following script and click Create.
    Note: The below script is a sample and for reference only, update the script as per your custom field ID's and other changes as necessary.
    from java.lang import System, Class, String, StringBuffer
    from java.util import HashMap
    from psdi.iface.router import HTTPHandler
    from com.ibm.json.java import JSONObject, JSONArray
    from org.apache.http import HttpEntity, HttpHeaders, HttpResponse, HttpVersion
    from org.apache.http.client.methods import HttpPost
    from org.apache.http.entity import StringEntity
    from org.apache.http.impl.client import DefaultHttpClient
    from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote, MboConstants, SqlFormat
    from psdi.security import UserInfo
    from psdi.server import MXServer
    from com.google.gson import JsonParser, JsonElement, JsonObject, Gson
    
    # Fetch current user organization ID
    def getOrgData():
        try:
            mxServer = MXServer.getMXServer()
            userInfo = mxServer.getSystemUserInfo()
            orgSet = mxServer.getMboSet("ORGANIZATION", userInfo)
            org = orgSet.moveFirst()
            
            if orgSet and org:
                orgid = org.getString("ORGID")
                return orgid
                
            else:
                raise Exception("Organization not found for the user.")
        except Exception as e:
            print("Error in getOrgData")
            return None
    
    def createParent(setdata):
        try:
            userinfo = MXServer.getMXServer().getSystemUserInfo()
            newClassStrSet = MXServer.getMXServer().getMboSet("CLASSUSEWITH", userinfo)
            newClassStr = newClassStrSet.add()
            newClassStr.setValue("DESCRIPTION", setdata["description"])
            newClassStr.setValue("OBJECTNAME", "ASSET")
            newClassStr.setValue("OBJECTVALUE", "ASSET")
            newClassStr.setValue("CLASSSTRUCTUREID", setdata["classid"])
            newClassStrSet.save()
            return "Done"
        except Exception as e:
            print("Error in creating use with object")
            return "Failed"
    
    def checkDuplicateClassUse(setdata):
        try:
            mxServer = MXServer.getMXServer()
            classSet = mxServer.getMboSet("CLASSUSEWITH", mxServer.getSystemUserInfo())
            start=1
            end=classSet.count()
            map=HashMap()
            classMbo = classSet.add()
            desckey=setdata["classid"]
            for i in range(start, end+1):
                classData = classSet.getMbo(i);
                classdesc = classData.getString("CLASSSTRUCTUREID")
                map.put(classdesc,"CLASSSTRUCTUREID")
            
            if desckey not in map.keys():
                createParent(setdata)
                return;
            else:
                print("Class structure  already exists.")
        except Exception as e:
            print("Error in checkDuplicateClassUse: {e}")
    
    def createClassStr(setdata):
        try:
            userinfo = MXServer.getMXServer().getSystemUserInfo()
            newClassStrSet = MXServer.getMXServer().getMboSet("CLASSSTRUCTURE", userinfo)
            newClassStr = newClassStrSet.add()
            newClassStr.setValue("DESCRIPTION", setdata["description"])
            newClassStr.setValue("CLASSSTRUCTUREID", setdata["classid"])
            newClassStr.setValue("CLASSIFICATIONID", setdata["classid"])
            newClassStr.setValue("GENASSETDESC", 0)
            newClassStr.setValue("ORGID", getOrgData())
            newClassStrSet.save()
            checkDuplicateClassUse(setdata)
            return "done"
        except Exception as e:
            print("Error in creating Classstructure: {e}")
            return "Failed"
    
    def checkClassStrExistance(setdata):
        try:
            userinfo = MXServer.getMXServer().getSystemUserInfo()
            newClassStrSet = MXServer.getMXServer().getMboSet("CLASSSTRUCTURE", userinfo)
            start=1
            end=newClassStrSet.count()
            map=HashMap()
            classMbo = newClassStrSet.add()
            
            # for item in classifications:
            desckey=setdata["classid"]
            for i in range(start, end+1):
                classData = newClassStrSet.getMbo(i);
                classdesc = classData.getString("CLASSSTRUCTUREID")
                map.put(classdesc,"CLASSSTRUCTUREID")
                      
            if desckey not in map.keys():
                createClassStr(setdata) 
            else:
                print("Class structure with ID already exists.")
        except Exception as e:
            print("Error in checkClassStrExistance: {e}")
    
    def createClassifyData(setdata):
        try:
            userinfo = MXServer.getMXServer().getSystemUserInfo()
            classifySet = MXServer.getMXServer().getMboSet("CLASSIFICATION", userinfo)
            newClassify = classifySet.add()
            newClassify.setValue("DESCRIPTION", setdata["description"])
            newClassify.setValue("CLASSIFICATIONID", setdata["classid"])
            newClassify.setValue("ORGID", getOrgData())
            classifySet.save()
            checkClassStrExistance(setdata)
            return "done"
        except Exception as e:
            print("Error in creating classification")
            return "Failed"
    
    def checkExistance():
        try:
            mxServer = MXServer.getMXServer()
            classSet = mxServer.getMboSet("CLASSIFICATION", mxServer.getSystemUserInfo())
            start=1
            end=classSet.count()
            map=HashMap()
            classMbo = classSet.add()
            # Create IT, OT, and IOT parent classifications
            classifications = [
                {"classid": "CLAROTY_IT", "description": "Claroty IT"},
                {"classid": "CLAROTY_OT", "description": "Claroty OT"},
                {"classid": "CLAROTY_IOT", "description": "Claroty IOT"}
            ]
            for item in classifications:
                desckey=item["classid"]
            
                for i in range(start, end+1):
                    classData = classSet.getMbo(i);
                    classdesc = classData.getString("CLASSIFICATIONID")
                    map.put(classdesc,"CLASSIFICATIONID")
                
                if desckey not in map.keys():
                    createClassifyData(item)  
                else:
                    print "Classification already exist"
            return "Done"
            
        except Exception as e:
            print("Already exist")
            return "Failed"
    
    # Run the existence check process
    checkExistance()
    
  5. Save your changes.