1

I have a Jython 2.7 script that receives a URL and uses the parameters/values in the URL to create or update records.

  • Example URL: http://server:host/maximo/oslc/script/CREATEWO?&wonum=WO0001&description=Legacy&classstructureid=1666&wopriority=1&worktype=CM

Details:

  1. Receive the URL and put the parameters/values in variables:
from psdi.server import MXServer
from psdi.mbo import MboSet

resp = {}
wonum =             request.getQueryParam("wonum")
description =       request.getQueryParam("description")
classstructureid =  request.getQueryParam("classstructureid")
wopriority =        request.getQueryParam("wopriority")
worktype =          request.getQueryParam("worktype")
  1. Some lines that aren't relevant to the question:
woset = MXServer.getMXServer().getMboSet("workorder",request.getUserInfo())
whereClause = "wonum= '" + wonum + "'"

woset.setWhere(whereClause)
woset.reset()
woMbo = woset.moveFirst()
  1. Then use the values to either create a new record or update an existing record:
#If workorder already exists, update it:
if woMbo is not None:
  woMbo.setValue("description", description)
  woMbo.setValue("classstructureid", classstructureid)
  woMbo.setValue("wopriority", wopriority)
  woMbo.setValue("worktype", worktype)
  woset.save()
  woset.clear()
  woset.close()
  resp[0]='Updated workorder ' + wonum
#Else, create a new workorder
else:
  woMbo=woset.add()
  woMbo.setValue("wonum",wonum)
  woMbo.setValue("description", description)
  woMbo.setValue("classstructureid", classstructureid)
  woMbo.setValue("wopriority", wopriority)
  woMbo.setValue("worktype", worktype)
  woset.save()
  woset.clear()
  woset.close()
  resp[0]='Created workorder ' + wonum
responseBody =resp[0]

Question:

Unfortunately, the field names/values are hardcoded in 3 different places in the script.

I would like to enhance the script so that it is dynamic -- not hardcoded.

  • In other words, it would be great if the script could accept a list of parameters/values and simply loop through them to update or create a record in the respective fields.

Is it possible to do this?

User1974
  • 276
  • 1
  • 17
  • 63

2 Answers2

3

You're using the Maximo Next Gen. REST API to execute an automation script that accepts an HTTP request with parameters and creates or updates a Work Order in the system. You want to make your script more generic (presumably to accept more paramaters for the created/updated work order) and/or other mbo's.

This can be achieved without developing automation scripts and just using the Next Gen. API you're already using to execute the script. The API already accepts create & update requests on the mxwo object structure with the ability to use all the fields, child objects, etc.

https://developer.ibm.com/static/site-id/155/maximodev/restguide/Maximo_Nextgen_REST_API.html#_creating_and_updating_resources

Maximo.Wiki
  • 631
  • 5
  • 17
2

Assuming you are working always with the same query parameters, rather than define variables, loop through a list of strings and put them as key-value pairs

To populate

items = ["wonum", "description"]
resp = {k: request.getQueryParam(k) for k in items}

Then to set

for i in items:
    woMbo.setValue(i, resp[i])

Otherwise, you are looking for URL parsing and the getQuery method, followed by a split("="), giving you ["wonum", "WO0001", "description", "Legacy"], for example, and you can loop over every other element to get you dynamic entries

l = ["wonum", "WO0001", "description", "Legacy"]
for i in range(0, len(l)-1, 2):
    print(f'key:{l[i]}\tvalue:{l[i+1]}')

key:wonum   value:WO0001
key:description value:Legacy

Note: This is subject to SQL injection attacks, and should be fixed

whereClause = "wonum= '" + wonum + "'"
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245