Tuesday 19 January 2021

Reuseable Scripts - Invoke a Script from a Script - Jython | Maximo

 Question:

  1. Can we invoke a script from another script?
  2. Is it possible to reuse the existing Script?
  3. Can we make the Global function and call it in the script when required?

Solution:

My answer is yes to all the above questions, let's start with this example. 

--- " Do this job within 5 business days (excluding weekends (Friday, Saturday))! "

Okay. Today is 19-Jan, what is my, etc after 5 days.


we will handle this with 2 scripts: 

  1. a library script to take the parameter of date + days and gives an output. 
  2. our script from where we will call the library script. 
1. Create a Library Script and name it any like "LS-GETNEXTBUSINESSDAY"
copy the paste the code below into the body of it. 

from java.util import Calendar
cal = Calendar.getInstance();
cal.setTime(d)
loop=1
while(loop<=n) :
    cal.add(cal.DATE, +1)
    if (cal.get(cal.DAY_OF_WEEK)) not in [cal.FRIDAY,cal.SATURDAY] : 
        loop=loop+1
vNextBDay = cal.getTime()

2.  Call this function from another script with 2 parameters d, n ( date, days)

In my example, I have created my test script on Asset Object Launch Point for a specific asset for testing. you can use it anywhere in your script. 

from java.util import HashMap
vAsset = mbo.getString("ASSETNUM")
if vAsset == 'A-74061-D702003-10002':
    vParams = HashMap()
    vParams.put("d", mbo.getDate("changedate")) #date or u can take currentdate ()
    vParams.put("n"5#5 days
    service.invokeScript("LS-GETNEXTBUSINESSDAY",vParams)
    vEtc = vParams.get("vNextBDay")
    service.error(str(etc),"")


and we are done :). 



cal.getTime() will be use to take current date. 

HashMap() is used to bind the parameters because of the service.invokeScript method takes hashmap data structure as input.


Reference: IBM Knowledge Center


No comments:

Post a Comment