Maximo

Maximo

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

 View Only
  • 1.  Invoke autoscript via URL to create WO

    Posted Tue June 16, 2020 07:52 PM
    I have a URL that invokes a Jython automation script (related post here):
    http://server:host/maximo/oslc/script/CREATEWO?_lid=wilson&_lpwd=wilson&v_WONUM=LWO0001

    The script is called CREATEWO (it does not have any launch points or bind variables):

    resp = {};
    v_WONUM = request.getQueryParam("v_WONUM")
    responseBody="Here's some fake text about WO: " + v_WONUM + " from the Maximo automation script."

    The script runs successfully; it takes in the WONUM and passes back a message.

    Question:

    I want to enhance the script so that it creates a new WO in Maximo using the WONUM that was passed in.

    Is it possible to do this?

    ------------------------------
    Thanks
    ------------------------------

    #Maximo
    #AssetandFacilitiesManagement


  • 2.  RE: Invoke autoscript via URL to create WO

    Posted Wed June 17, 2020 07:12 AM
    Since your script won't have a MBO at this point, to go through the process of creating a WO you first must get a set from MXServer and then add on to the set. For example:

    from psdi.server import MXServer
    maxServer=MXServer.getMXServer()
    woSet=maxServer.getMboSet("WORKORDER",maxServer.getSystemUserInfo())
    woSet.setWhere("1=2")
    woSet.reset()
    woMbo=woSet.add()
    woMbo.setValue("WONUM",v_WONUM)
    woSet.save()

    You would at a minimum need to provide an asset, location, or GL account on the WO and anything else that is flagged as required in your system (including sometimes conditional data restrictions that make a field required based on the values in some other fields). And unless you really need to go about it this way for some reason, it'd be better to create a WO using an object structure over trying to implement the logic you need here to capture the fields and set the corresponding attributes in Maximo. There's a lot of capabilities in the integration framework (including special processing you can do with automation scripts).

    ------------------------------
    Steven Shull
    Director of Development
    Projetech Inc
    Cincinnati OH
    ------------------------------



  • 3.  RE: Invoke autoscript via URL to create WO

    Posted Wed June 17, 2020 10:51 AM
    So there are a few ways you could tackle this, but the simplest by far is to just use the out of the box MXWO integration object.  Making a POST request to the server at

    http://server.name.here/maximo/oslc/os/MXWO

    with a JSON of

    {
    	"wonum": "54321",
    	"description": "Demo Work Order",
    	"siteid": "BEDFORD"
    }


    Will create a new work order with the specified work order number.  Be aware that it is your responsibility to check that that work order number has not been used previously otherwise you will get the following error.

    BMXAA4129E - The record for Site=BEDFORD, Work Order=54321 already exists. Ensure that the key value for the given record is unique.


    If you have custom logic you want to apply you can do so using the integration pipeline Automation Scripts. 

    You can do what Steven suggested, but note that he is using the getSystemUserInfo() method to get the system user to create the work order, which means that your work order will be created as the system user and not the actual user that is performing the action.  This may impact other conditional expression behavior and bypass any security constraints you have in place.

    Also, since the system user never logs out you need to be very careful to close any MboSets opened by that user otherwise they will stay in memory until server restarts, in other words you will create a memory leak.







    ------------------------------
    Jason VenHuizen
    ------------------------------



  • 4.  RE: Invoke autoscript via URL to create WO

    Posted Mon July 06, 2020 08:28 AM
    Edited by System Admin Wed March 22, 2023 11:51 AM

    Thanks again to Steven and Jason for your input.

    I plan to pursue the OOB box option that you mentioned: "Create a WO using an object structure" ...aka... "MXWO integration object".



    On a side note, I did end up pursuing the custom script option, just as a professional development exercise. It was a relatively simple example where I could learn a bit about automation scripting.

    Again, I'm planning to switch to the OOB method, but for now, here's the code I came up with:

    I would be happy to hear about any flaws in the script -- or improvements that could be made (knowing full-well that this is not the preferred method).

    #Stack Exchange Code Review: Create or update record via HTTP request
    #https://codereview.stackexchange.com/questions/245073/part-2-create-or-update-record-via-http-request
    
    from psdi.mbo import SqlFormat
    from psdi.server import MXServer
    from psdi.mbo import MboSet
    
    IGNORE_RULES = 2L
    
    paramdict = {
        p[2:]: request.getQueryParam(p)
        for p in request.getQueryParams()
        if p.startswith('f_')
    }
    woset = MXServer.getMXServer().getMboSet("workorder",request.getUserInfo())
    
    try:
        #Prevents SQL injection
        sqf = SqlFormat("wonum=:1")
        sqf.setObject(1,"WORKORDER","WONUM",request.getQueryParam("f_wonum"))
    
        woset.setWhere(sqf.format())
        woset.reset()
        woMbo = woset.moveFirst()
    
        if woMbo is None:
            woMbo=woset.add()
            verb = 'Created'
        else:
            verb = 'Updated'
    
        for k,v in paramdict.items():
            woMbo.setValue(k,v,IGNORE_RULES)
        resp = verb + ' workorder ' + request.getQueryParam("f_wonum")
        woset.save()
        woset.clear()
    finally:
        woset.close()
    
    responseBody = resp

     

    Some context about ignoring the rules via 2L can be found here:
    Change WO's ASSETNUM via autoscript?