Maximo

 View Only
Expand all | Collapse all

Auto-script - trouble getting value from child object in inbound REST API message

  • 1.  Auto-script - trouble getting value from child object in inbound REST API message

    Posted Thu January 19, 2023 01:17 PM
    Edited by System Tue August 22, 2023 04:42 PM
    Hi Folks!

    I'm trying to manipulate an inbound message using the "beforeProcess" in an automation script, but can't quite accomplish what I need -- hoping one of the brilliant people on here can assist :)

    I've got an object structure that includes SR with TKSERVICEADDRESS as a child object. Here's the format of an inbound message...

    {"externalrecid":"ABC12345",
    "tkserviceaddress": [
      {
        "latitudey": 45.123,
        "longitudex": -85.123,
        "formattedaddress": "1234 MAIN ST"
      }
     ]
    }

    I want to get the "formattedaddress" from the child object and use it to populate the "description" attribute on the main object so that it looks like this:

    {"externalrecid":"ABC12345",
    "description":"SR near 1234 MAIN ST",
    "tkserviceaddress": [
      {
        "latitudey": 45.123,
        "longitudex": -85.123,
        "formattedaddress": "1234 MAIN ST"
      }
     ]
    }

    I can add a description on the message as shown below.   But I don't know the syntax to get in to the child object and get the value of "formattedaddress" so that I can use it in the SR description.

    Any help is greatly appreciated!!

    def beforeProcess(ctx):
        struc = ctx.getData()
        srDescrip = "SR"  #need to get "formattedaddress" value and add it here
        struc.setCurrentData("description",srDescrip)


    Note, I've tried a few things shown below to get the value from formatted address, but none have worked...

    struc.getCurrentData("formattedaddress")
    struc.getCurrentData("tkserviceaddress")[0].("formattedaddress")
    struc.getChildrenData()     #didnt work because struc is json

    Thanks,

    ------------------------------
    Ryan Coghlin
    ------------------------------



    #AssetandFacilitiesManagement
    #Maximo
    #MaximoIntegrationandScripting


  • 2.  RE: Auto-script - trouble getting value from child object in inbound REST API message
    Best Answer

    Posted Fri January 20, 2023 01:40 AM
    Hello Ryan,

    Could you pls try this !!

    def beforeProcess(ctx):
        struc = ctx.getData()
        child = struc.getChildrenData('TKSERVICEADDRESS')
        formattedAddress = child[0].getCurrentData("FORMATTEDADDRESS")
        #srDescrip = "SR"  #need to get "formattedaddress" value and add it here
        struc.setCurrentData("description",formattedAddress)​


    ------------------------------
    Kalaiselvan Matheswaran
    ------------------------------



  • 3.  RE: Auto-script - trouble getting value from child object in inbound REST API message

    Posted Fri January 20, 2023 09:47 AM
    Success!  Thank you Kalaiselvan. Your suggestion worked perfectly.

    So my problem was that I didn't include the name of the child object when using "getChildrenData()"

    Before you "rescued" me, I did a workaround using "JSONObject" to get the value (shown below).   But your approach above is definitely the correct one.

    Thanks again!

    from com.ibm.json.java import JSONObject
    
    def beforeProcess(ctx):
        struc = ctx.getData()
        json = JSONObject.parse(str(struc))
        srvAddr = json["tkserviceaddress"][0]
        frmtAddr = srvAddr["formattedaddress"]
        struc.setCurrentData("description",frmtAddr)





    ------------------------------
    Ryan Coghlin
    ------------------------------