Maximo Open Forum

 View Only
  • 1.  Comparison of three dates in Maximo Automation Scripting

    Posted 03-01-2021 02:38
    Edited by Harshavardhan Kamepalli 03-01-2021 02:39
    Hi there,
    I have a requirement to compare a date in Service Request application with two other dates in child table naming, start date and end date.
    Here, Child table(in this case, BOOKING) has multiple rows, each row consisting Start date and End date. Now, a validation has to be done on date field in Service Request application by comparing it with each row of Start Date and End Date.

    To achieve this,
    1) Created a relationship between SR and BOOKING.
    2) Created attribute launch point on the date field of Service Request and written the following script:

    from psdi.server import MXServer
    from psdi.mbo import Mbo
    from java.util import Calendar
    from java.util import Date
    description=mbo.getString("DESCRIPTION")
    if (description=="Use of IT E-Center"):
    offstartdate=mbo.getDate("OFFSTARTDATE")
    cal=Calendar.getInstance()
    datetime=cal.getTime()
    if(offstartdate <= datetime):
    errorkey = 'Start Date should be greater than current date'
    errorgroup = 'system'
    else:
    offbooking=mbo.getMboSet("BOOK")
    i=0
    records=offbooking.getMbo(i)
    while(records is not None):
    i=i+1
    startdate=records.getDate("STARTDATE")
    enddate=records.getDate("ENDDATE")
    if (startdate <= offstartdate <= enddate):
    errorkey = 'Start Date is between booked date'
    errorgroup = 'system'
    break
    records=offbooking.getMbo(i)

    Here,
    OFFSTARTDATE: is the date field which has to be validated
    STARTDATE and ENDDATE: are the fields with which OFFSTARTDATE has to be compared with
    BOOK: is name of the relationship between SR and BOOKING tables

    We had no luck because the script did not work, So any help here is appreciated.
    #Customizations

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------


  • 2.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-02-2021 08:51
    Unfortunately the formatting was lost so I can't tell exactly how things are indented to verify the logic throughout. Next to the underline control there should be an Insert/Edit code snippet that will preserve your formatting. I'm assuming it looks like the code I formatted below.

    from psdi.server import MXServer
    from psdi.mbo import Mbo
    from java.util import Calendar
    from java.util import Date
    description=mbo.getString("DESCRIPTION")
    if (description=="Use of IT E-Center"):
        offstartdate=mbo.getDate("OFFSTARTDATE")
        cal=Calendar.getInstance()
        datetime=cal.getTime()
        if(offstartdate <= datetime):
            errorkey = 'Start Date should be greater than current date'
            errorgroup = 'system'
    else:
        offbooking=mbo.getMboSet("BOOK")
        i=0
        records=offbooking.getMbo(i)
        while(records is not None):
            i=i+1
            startdate=records.getDate("STARTDATE")
            enddate=records.getDate("ENDDATE")
            if (startdate <= offstartdate <= enddate):
                errorkey = 'Start Date is between booked date'
                errorgroup = 'system'
                break
            records=offbooking.getMbo(i)​

    Assuming that's the case, your offstartdate is a local variable inside of your if statement only. So when you make it down the comparison there is no way for it to evaluate to true as the variable is undeclared at that point. Simply moving that get outside of the if statement (close to where you put description) should address that issue. 

    I'd also suggest utilizing service.error() instead of errorkey and errorgroup. It's a little bit cleaner but more importantly it stops execution of your script.

    From there you should define actual messages in the Database Configuration application. These parameters are supposed to correspond to msggroup and msgkey on the MAXMESSAGES table. If they don't exist it'll still come through to the user but in group#key format which doesn't look good. In my example below I've use placeholders that can be replaced.

    I think this should do what you're looking for based on what you've provided. I cleaned up the imports a bit and did date differently as having MXServer imported you can easily get the current date without needing to utilize calendars.  

    from psdi.server import MXServer
    
    description=mbo.getString("DESCRIPTION")
    offstartdate=mbo.getDate("OFFSTARTDATE")
    
    if (description=="Use of IT E-Center"):
        if(offstartdate <= MXServer.getMXServer().getDate()):
            service.error("emx","startLessThanCurrent")
    else:
        offbooking=mbo.getMboSet("BOOK")
        i=0
        records=offbooking.getMbo(i)
        while records:
            i=i+1
            startdate=records.getDate("STARTDATE")
            enddate=records.getDate("ENDDATE")
            if (startdate <= offstartdate <= enddate):
                service.error("emx","dateBetweenBookedDates")
            records=offbooking.getMbo(i)​


    ------------------------------
    Steven Shull
    Projetech Inc.
    ------------------------------



  • 3.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-03-2021 00:18
    Edited by Harshavardhan Kamepalli 03-03-2021 01:01
    Hi Steven,
    Sorry for the lost formatting and Here is the correct formatting of the Script:

    from psdi.server import MXServer
    from psdi.mbo import Mbo
    from java.util import Calendar
    from java.util import Date
    description=mbo.getString("DESCRIPTION")
    if (description=="Use of IT E-Center"):
        offstartdate=mbo.getDate("OFFSTARTDATE")
        cal=Calendar.getInstance()
        datetime=cal.getTime()
        if(offstartdate <= datetime):
            errorkey = 'Start Date should be greater than current date'
            errorgroup = 'system'
        else:
            offbooking=mbo.getMboSet("BOOK")
    	    i=0
    	    records=offbooking.getMbo(i)
    	    while(records is not None):
    	        i=i+1
    	        startdate=records.getDate("STARTDATE")
    	        enddate=records.getDate("ENDDATE")
    	        if (startdate <= offstartdate <= enddate):
    	        errorkey = 'Start Date is between booked date'
    	        errorgroup = 'system'
    	        break
    	    records=offbooking.getMbo(i)

    I Changed your code to this to match my format:
    from psdi.server import MXServer
    
    description=mbo.getString("DESCRIPTION")
    offstartdate=mbo.getDate("OFFSTARTDATE")
    
    if (description=="Use of IT E-Center"):
        if(offstartdate <= MXServer.getMXServer().getDate()):
            service.error("emx","startLessThanCurrent")
        else:
            offbooking=mbo.getMboSet("BOOK")
            i=0
            records=offbooking.getMbo(i)
            while(records is not None):
                i=i+1
                startdate=records.getDate("STARTDATE")
                enddate=records.getDate("ENDDATE")
                if (startdate <= offstartdate <= enddate):
                    service.error("emx","dateBetweenBookedDates")
            records=offbooking.getMbo(i)​


    The "else" code did not work in this case, the date value does not gets validated.
    and When I use the "if(startdate <= offstartdate)", the validation works but not with three dates i.e. "if(startdate <= offstartdate <= enddate)" it does not work.

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------



  • 4.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-03-2021 08:33
    Hi,

    In coding you cannot do x < y < z.  You must separate them.

    Your if line should be:
    if (startdate <= offstartdate and offstartdate <= enddate):​


    Regards
    Dave S



    ------------------------------
    David Shaw
    SRO Solutions
    ------------------------------



  • 5.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-03-2021 08:38
    I thought it was weird but didn't get a syntax error in Visual Studio code so I thought maybe there was a streamlined way to do it in Python.

    ------------------------------
    Steven Shull
    Projetech Inc.
    ------------------------------



  • 6.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-03-2021 23:52
    Thanks David, really appreciate you taking time to reply.
    But it did not work.
    Tried these cases too:
    "if((startdate <= offstartdate) and (offstartdate <= enddate)):"
    "if((offstartdate >= startdate) and (offstartdate <= enddate)):"
    "if((startdate <= offstartdate) >= enddate):"
    "if(startdate <= offstartdate):
        if(offstartdate <= enddate):"

    But, none of these work and am struck at this very condition. 


    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------



  • 7.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-03-2021 09:26
    Edited by David Shaw 03-03-2021 09:29
    It may not be the date validation.  As Steve said (and a quick google later) apparently you can check dates like that. However your final line is one indent too little.  Its outside your while loop so needs to be tabbed one more across or it will loop forever.

    ------------------------------
    David Shaw
    SRO Solutions
    ------------------------------



  • 8.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-04-2021 00:07
    Thanks Shaw,
    It was the Indent issue.
    Pushed it under the while loop and it works fine now.

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------



  • 9.  RE: Comparison of three dates in Maximo Automation Scripting

    Posted 03-04-2021 00:56
    I Should have just followed your code Steve, your formatting was right on point.​

    ------------------------------
    Harshavardhan Kamepalli
    Eidiko System Integrators
    ------------------------------