Saturday, August 27, 2016

Automation Script to add days to a Datetime Field.

Adding Days to date.
This Seems to be a very simple requirement but It's very tricky. In the following query , I am trying to add some number of days to a DATETIME field and set that date.
For Example :- In Workorder application, If the TARGETSTARTDATE is 10 March 2016. And I want to set the TARGETFINISHDATE to 10 Days later. So my TARGETFINISHDATE would be 20 March 2016


Script Language:Jython
Variable: startdate binding to TARGETSTARTDATE


from java.util import Calendar

startdate =  mbo.getDate("TARGETSTARTDATE ")
cal=Calendar.getInstance()
cal.setTime(startdate )
cal.add(Calendar.DATE, +10)
date=cal.getTime()
mbo.setValue("TARGETFINISHDATE ",date,MboConstants.NOACCESSCHECK + MboConstants.NOVALIDATION_AND_NOACTION)

As the script imports java Calendar classes , an instance of Calender object is obtained.
The getInstance()  method returns an object that is based on the current time in the default time-zone and default locale of the current JVM where Maximo is running.However, The Calendar object’s time is being set to the Workorder’s targetstartdate attribute value using the Calendar class’ setTime() method.Input variable ‘startdate’ is the parameter to the setTime() method. The scripting framework recognizes that the input variable is bound to a Maximo attribute of DATETIME or DATE type and automatically creates a Java Date object to represent the targetstartdate from the WO record. Thus, input variable startdate is directly passed to setTime().

Let me know if it works.

7 comments:

  1. Thanks this blog was really help full!!

    ReplyDelete
  2. thanks for this post.
    how about, if I need to add hours to a date?

    ReplyDelete
  3. Thanks! It was really helpful. You should post more often!

    ReplyDelete
  4. cal1.getTime();
    cal.setTime(bd1);
    cal.add(cal.YEAR , - cal1)
    its not working how can i do it ?

    ReplyDelete
  5. elif mc < mb :
    yCurrentDate = SimpleDateFormat("yyyy").format(Date())
    yc = yCurrentDate
    cal.setTime(bd1);
    cal.add(cal.YEAR , - yc)
    bd = cal.getTime();
    mbo.setValue("CDATE",bd)
    age= SimpleDateFormat("yy").format(mbo.getDate("CDATE"))
    mbo.setValue("AGE",age)
    its not working why ?

    ReplyDelete
  6. elif mc < mb :
    cal1 =Calendar.getInstance()
    cal1.getTime();
    cal.setTime(bd1);
    cal.add(cal.YEAR , - cal1.YEAR)
    bd = cal.getTime();
    mbo.setValue("CDATE",bd)
    age= SimpleDateFormat("yy").format(mbo.getDate("CDATE"))
    mbo.setValue("AGE",age)
    this is working but the result is 93 why ?

    ReplyDelete