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
------------------------------
Original Message:
Sent: Sun November 14, 2021 08:07 PM
From: User1971
Subject: Execute script on-demand: Can't use library scripts? (global service variable)
Here's the script in text form:
from psdi.mbo import MboConstantsfrom java.util import HashMapfrom com.ibm.json.java import JSONObject#remember to use the "row count only" parameter in the URLs: &returnCountOnly=trueurl = "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_valprint 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.
Original Message:
Sent: Sun November 14, 2021 08:02 PM
From: User1971
Subject: Execute script on-demand: Can't use library scripts? (global service variable)
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 that references 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_countNameError: global name 'service' is not defined
How can I avoid getting that error?
Thanks.
#AssetandFacilitiesManagement
#Maximo