Maximo

Maximo

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

 View Only
  • 1.  Clean Jython: If field is null

    Posted Tue November 09, 2021 04:34 PM
    Edited by System Admin Wed March 22, 2023 11:52 AM
    MAM 7.6.1.2, Jython, Oracle 19c:

    I have a Jython automation script that checks to see if CLASSSTRUCTURE.DESCRIPTION is null:

    if not mbo.getString("DESCRIPTION"):
        mbo.setValue("DESCRIPTION", mbo.getString("CLASSIFICATIONID"))

    As a novice, I find if not mbo.getString("DESCRIPTION"): to be unintuitive to read.
    I'd rather use something like if mbo.isNull("DESCRIPTION"): . I find that easier to read.

    Question:
    What is a good/intuitive way to check for nulls via Jython? Do I need to worry about empty strings?
    My best guess is that Python variables can have empty strings (''), but Oracle VARCHAR2 columns can't have empty strings ('' is automatically converted to null).




    [edited for clarity]
    Details:

    As mentioned, I don't think it's possible to store an empty string like '' (no space) in VARCHAR2 columns in Oracle tables (the Maximo db uses the VARCHAR2 datatype for text). Oracle automatically converts '' to null when inserting data into a table.


    As an example, I mocked up some sample data in a online database testing website called "db<>fiddle":
    https://dbfiddle.uk/?rdbms=oracle_18&fiddle=e6fda0c32c59e027da6015c838b49f7a

    If I understand correctly, the test confirms that Oracle automatically converts '' to null when inserting data into a VARCHAR2 column.





    #Maximo
    #AssetandFacilitiesManagement


  • 2.  RE: Clean Jython: If field is null

    Posted Wed November 10, 2021 04:30 AM
    H,
    there are several things to consider.
    "If a variable is null (i.e. it has no value) then it can only be checked using variable is None.

    If var is None:

    Some string functions return an empty string so it is important to check for ""

    If var = "":

    If the variable isn't defined then errors like this can be generated when the script tries to use a variable which is set to null."
    This is an extract from the Vetasi automation script training course.

    The IBM Javadocs for the MBO set shows that there is a method to check if an attribute is null:
    "

    isNull(java.lang.String attributeName)
    Determines whether the attribute value identified by name within the current object is null.

    "

    It IS possible to explicitly set a value to an empty string in an ORACLE column.
    A common mistake is to assume that "" is null.

    I hope this helps.



    ------------------------------
    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/
    ------------------------------



  • 3.  RE: Clean Jython: If field is null

    Posted Wed November 10, 2021 08:35 AM
    yes ORACLE does treat '' as null.
    I forget the exact syntax but people have been caught out using - I think it is when people use things like "" rather than explicitly using ''

    ------------------------------
    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/
    ------------------------------



  • 4.  RE: Clean Jython: If field is null

    Posted Wed November 10, 2021 09:07 AM
    Python variables can be evaluated as truthy/falsy like Javascript. For example:

    description = None
    if description:
      logger.debug('variable description is not null or empty')
    else:
      logger.debug('variable description is null or empty')

    description = ''
    if description:
      logger.debug('variable description is not null or empty')
    else:
      logger.debug('variable description is null or empty')

    Those two conditional blocks should evaluate to false. It should be noted that Maximo tends to do a good job of not saving empty strings to the database, which makes the isNull(attribute) method effective. A safe way of evaluating would be:

    description = mbo.getString("DESCRIPTION")
    if description:
      logger.debug('variable description is not null or empty')
    else:
      logger.debug('variable description is null or empty')

    Hope this helps,


    ------------------------------
    Alex Walter
    A3J Group
    ------------------------------