Maximo

Maximo

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

 View Only
  • 1.  Automation Script to do equivalent of "if exists"

    Posted Thu November 04, 2021 01:19 PM
    I've had a request to set the value on a field on a Work Order if the Location on the WO has a specific ancestor (don't want to use Lochierachy because a location could be a grandchild or more). I got the entire query setup except for the check to see if the Location on the work order exists in the specific location hierarchy. 

    from psdi.server import MXServer
    from psdi.mbo import MboConstants
    
    CUR_LOCID = mbo.getString("LOCATION")
    CUR_SITEID = mbo.getString("SITEID")
    
    
    if CUR_SITEID == 'ACME' and exists(select null from locancestor where location = workorder.location and siteid = workorder.siteid and ancestor='SPEC_LOCID') 
        mbo.setValue("WORISK", 3)​


    My challenge is finding the equivalent of the "if exists" portion in the automation script? Looking for any suggestions. Thanks!

    ------------------------------
    Jason Verly
    Reliability Engineering Manager
    Agropur US
    Le Sueur MN
    ------------------------------


    #AssetandFacilitiesManagement
    #Maximo


  • 2.  RE: Automation Script to do equivalent of "if exists"

    Posted Thu November 04, 2021 02:13 PM
    How about in a relationship?

    from psdi.server import MXServer
    from psdi.mbo import MboConstants

    CUR_LOCID = mbo.getString("LOCATION")
    CUR_SITEID = mbo.getString("SITEID")

    #Example
    #Create relationship - Name: CUR_LOCANCESTOR
    #Parent: WORKORDER Child: LOCATIONS
    #Where Clause:
    #location=:location and siteid=:siteid and exists(select null from locancestor where location = :location and siteid = :siteid and ancestor='SPEC_LOCID')

    if(!mbo.getMboSet("CUR_LOCANCESTOR").isEmpty()):
    mbo.setValue("WORISK", 3)​ ​

    ------------------------------
    Nivin Jacob George Senior Consultant
    Senior Consultant
    Praxis Solutions
    Kuwait
    ------------------------------



  • 3.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 05, 2021 09:25 AM
    I'm not sure if SPEC_LOCID is a hardcoded value or supposed to be dynamic so for now that assumes it's a hardcoded value. If this isn't something you plan to re-use elsewhere, IE it's just for this one script, I would get a set explicitly with the where clause you need. Example below.

    isEmpty() & notExist() return similar data but function differently. isEmpty() has caused database leaks so I try to avoid utilizing it. 

    locSet=mbo.getMboSet("$EMXLOCANCESTOR","LOCANCESTOR","location=:location and siteid=:siteid and ancestor='SPEC_LOCID'")
    if not locSet.notExist():

    ------------------------------
    Steven Shull
    ------------------------------



  • 4.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 05, 2021 10:10 AM
    If value 'SPEC_LOCID' was just and example of the Ancestor location value, so it would hard coded for a single automation script. If I understand the great advice, I came up with the following script:

    from psdi.server import MXServer
    from psdi.mbo import MboConstants
    
    CUR_LOCID = mbo.getString("LOCATION")
    CUR_SITEID = mbo.getString("SITEID")
    
    locSet=mbo.getMboSet("$EMXLOCANCESTOR","LOCANCESTOR","location=:location and siteid=:siteid and ancestor='SPEC_LOCID'")
    if locSet.Exist() and CUR_SITEID = 'ACME':
        mbo.setValue("WORISK", 3)​


    ------------------------------
    Jason Verly
    Reliability Engineering Manager
    Agropur US
    Le Sueur MN
    ------------------------------



  • 5.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 05, 2021 10:21 AM
    Slight modification to the script to a version that works for me:

    from psdi.server import MXServer
    from psdi.mbo import MboConstants
    
    CUR_LOCID = mbo.getString("LOCATION")
    CUR_SITEID = mbo.getString("SITEID")
    
    locSet=mbo.getMboSet("$EMXLOCANCESTOR","LOCANCESTOR","location=:location and siteid=:siteid and ancestor='SPEC_LOCID'")
    
    if not locSet.notExist() and CUR_SITEID == 'ACME':
        mbo.setValue("WORISK", '3')​


    ------------------------------
    Jason Verly
    Reliability Engineering Manager
    Agropur US
    Le Sueur MN
    ------------------------------



  • 6.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 05, 2021 12:14 PM
    On a side note: Does this RFE make sense?
    Need an exist() method in MboSetRemote()

    I don't know much about automation scripts, Java, or inheritance. So I don't know if that description makes sense or not.


  • 7.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 05, 2021 10:22 AM
    Unfortunately there's not an Exist method so you have to do a double negative (not locSet.notExist()).  But otherwise yes that looks correct.

    ------------------------------
    Steven Shull
    ------------------------------



  • 8.  RE: Automation Script to do equivalent of "if exists"

    Posted Mon November 08, 2021 01:50 PM

    Some background and detail for understanding.  First, Mbo design is that we do not close the set until it the MboSet has been discarded or all the rows have  been fetched.  As one sees in the UI, we do not fetch all the rows, so only when we try to fetch one beyond does the system know all has been fetched.  In the case if isEmpty() the code simply selects the first record.  That might be the only record or there might be thousands more.  So, that MboSet will remain active until someone explicitly closes it or the transaction is discarded.  In the case of notExists, we are functionally doing the exact same thing, but we are rearranging the SQL to hit the Dummy_table which should always have exactly 1 row.  In this case, we can close the set at the end as we either found 0 or 1 row and we are done.  (I think the naming here could have been better).


    As far as an RFE to add an exists() method, while the purist in me says "why not", the pragmatist in me says we have bigger fish to fry.

    -Steve



    ------------------------------
    Steve Hauptman
    ------------------------------



  • 9.  RE: Automation Script to do equivalent of "if exists"

    Posted Fri November 12, 2021 01:09 PM
    Steve - Thanks for taking the time to explain the difference between isEmpty and notExists methods

    ------------------------------
    Mark Robbins
    Support Lead/Technical Design Authority / IBM Champion 2017 & 2018 & 2019 & 2020 & 2021
    Vetasi Limited
    https://www.linkedin.com/pulse/maximo-support-advice-from-non-ibm-engineer-article-mark-robbins/
    ------------------------------