Maximo

Maximo

Come for answers, stay for best practices. All we're missing is you.

 View Only
  • 1.  Execute script on-demand: Can't use library scripts? (global service variable)

    Posted Sun November 14, 2021 08:03 PM
    Edited by System Admin Wed March 22, 2023 11:54 AM
    MAM 7.6.1.2:

    I have an automation script that I want to execute on-demand (for testing purposes).

    I can run a simple script like this:
    print "test"​


    That works fine.



    However, when I try to run a real script on-demand, referencing a library script (via the global service variable), then I get an error:


    Traceback (most recent call last):
      File "<iostream>", line 22, in <module>
      File "<iostream>", line 12, in fetch_row_count
    NameError: global name 'service' is not defined​


    How can I avoid getting that error?

    Thanks.
    #AssetandFacilitiesManagement
    #Maximo


  • 2.  RE: Execute script on-demand: Can't use library scripts? (global service variable)

    Posted Sun November 14, 2021 08:07 PM
    Here's the script in text form:

    from psdi.mbo import MboConstants
    from java.util import HashMap
    from com.ibm.json.java import JSONObject
    
    #remember to use the "row count only" parameter in the URLs: &returnCountOnly=true
    url = "https://vsgisapprd19:6443/arcgis/rest/services/Maximo/WO_COLLECT_POC/MapServer/1/query?where=CREATED_DATE%3CCURRENT_DATE&outFields=*&returnCountOnly=true&f=pjson"
    
    def fetch_row_count(url):
        # Get the JSON text from the map service.
        ctx = HashMap()
        ctx.put("url", url)
        service.invokeScript("CGLIBHTTPCLIENT", ctx)
        json_text = str(ctx.get("response"))
    
        # Parse the row count from the JSON text
        obj = JSONObject.parse(json_text)
        #parsed_val = obj.get("features")[0].get("attributes").get(gis_field_name)
        parsed_val = obj.get("count")
    
        return parsed_val
    
    print str(fetch_row_count(url))
    ​

    By the way, that script works fine if I don't run it on demand. For example, if I use a launch point to trigger the script in WOTRACK, then that works ok.
    It's just the "executing on-demand" thing that seems to have a problem.



  • 3.  RE: Execute script on-demand: Can't use library scripts? (global service variable)

    Posted Mon November 15, 2021 12:15 PM

    It is up to the caller of a script to provide all context for a script. "mbo" and "service" are examples of context that is set by Object, Attribute, Action and Condition launch points that call autoscripts. For On Demand scripts, there is no caller and therefore zero context -- you can only access variables that you create and initialize.

    Given the above, if you want to have an autoscript double as a library script and an on demand script, then it's up to you to provide some context if none was provided by a caller. In your case, if you want to use service, you will have to see if it's in your context and add it if it's not. Here is some sample code that will help with that.

    if "service" not in locals():
        from com.ibm.tivoli.maximo.script import ScriptService
        # ScriptService takes script name, launch point name, instance of Mbo
        service = ScriptService("MYSCRIPTNAME", None, None)



    ------------------------------
    Blessings,
    Jason Uppenborn
    Sr. Technical Maximo Consultant
    Ontracks Consulting
    ------------------------------



  • 4.  RE: Execute script on-demand: Can't use library scripts? (global service variable)

    Posted Mon November 15, 2021 12:12 PM
    Hello,

    When I was working on 7.6.0.8, I also found this issue. I created a ticket with IBM support and they told me that they don't support that "Test" feature anymore. It was meant to be used for some internal testing.

    By the way, you may be aware that you can execute a script without any launch point by using any Rest client tool (e.g. postman).

    The url to use is "https://<maximo_host>/maximo/api/script/<name of automation script>?lean=1&apikey=<apikey>". Add the request header "x-public-uri" with "https://<maximo_host>/maximo/api/"

    Pankaj Bhide




    ------------------------------
    Pankaj Bhide
    Computer Systems Engineer
    Berkeley National Laboratory
    Berkeley CA
    ------------------------------