Automation Script Template

How many times have you written an automation script and just can’t figure out how to import classes using jython?  I’m no jython expert, but I can’t count how many times I’ve googled how to format strings or concat a string and number to print to the logger.  Well, enough is enough!  I have create a template for my most commonly used functions in an automation script.  Please feel free to email me to add any comments or suggestions so we can make this template even better.

You can copy this and put it in your Maximo scripting library by going to “Automation Scripts” -> Select Action -> Create -> Script and name it TEMPLATE.  Then you will have this stored and you can refer to it whenever you need it.

Note: Copy the code to Notepad, then copy and paste from there.  If you copy direct from the browser to the code editor, you might get some weird characters that will crash your script when running.

This template includes:

  • Imports MboConstants
  • Imports MXServer
  • Helper function for ‘print’
  • Helper function for ‘getMboSet’
  • Help function to ‘sendEmail’
  • Help function to ‘raiseError’ which throws an application error based on some condition

Update (12/22/15):

  • Added ‘sendEmail’
  • Added ‘raiseError’ and ‘setError’
    • What’s the difference between ‘raiseError’ and ‘setError’?
      • When you use ‘setError’, the script runs to completion before the error is thrown.  At the end of the script, it detects the error flag and will throw the Maximo exception
      • You can use ‘raiseError’ to throw the exception when it is called stop the script execution

Update (12/21/15): Added a new helper ‘log’ function for printing that prints to the LONGDESCRIPTION field (I HATE having to go to the MX logs just to see print statements!!!)

Here’s the code:

from psdi.util import MXApplicationException
from psdi.mbo import MboConstants
from psdi.mbo import SqlFormat
from psdi.server import MXServer
from java.util import Calendar
from java.util import Date
from psdi.util.logging import MXLoggerFactory

### Examples: ###
#from psdi.app.workorder import WOSet
#from psdi.app.workorder import WO
 
#################################
# Helper function for sending an email
#################################
def sendEmail(toEmail, fromEmail, subject, body):
  MXServer.sendEMail(toEmail, fromEmail, subject, body)

#################################
# Helper function for printing
#################################
def log(e):
  print launchPoint + ":" + scriptName + (" >> %s" % e) + "\r\n"

#################################
# Helper function for logging
#################################
def setLogger(name):
  logger = MXLoggerFactory.getLogger(name) #Example: maximo.service.SCRIPT
def debug(e):
  logger.debug(e)
def info(e):
  logger.info(e)

#################################
# Another helper function for printing (use this if you want to use the LONGDESCRIPTION field on the MBO)
# Uncomment this section to use it
#################################
#def log(e):
# mbo.setValue("DESCRIPTION_LONGDESCRIPTION", mbo.getString("DESCRIPTION_LONGDESCRIPTION") + "%s<br />" % e)
## Reset long description before every run
#mbo.setValueNull("DESCRIPTION_LONGDESCRIPTION")

################################# 
# Helper function to set an error (see difference above from 'raiseError')
################################# 
def setError(errGroup, errKey, errParams):
  global errorkey,errorgroup,params
  errorkey=errKey
  errorgroup=errGroup
  params=errParams

################################# 
# Helper function to raise an application error (see difference above from 'setError')
################################# 
def raiseError(errGroup, errKey, errParams):
  raise MXApplicationException(errGroup, errKey, errParams)

################################# 
# Helper function to get any MboSet
################################# 
def getMboSet(objectName): 
  return MXServer.getMXServer().getMboSet(objectName, mbo.getUserInfo()) 

################################# 
# Script code begins here 
################################# 
print "\r\n" 
log("-------------- BEGIN SCRIPT ------------") 
log("My Automation Script Template") 
woSet = getMboSet("WORKORDER") 
log("Work Order Count: %s" % woSet.count()) 
log("-------------- END SCRIPT --------------")

The output looks like this:

[12/11/15 15:15:16:851 EST] 0000003d SystemOut     O 11 Dec 2015 15:15:16:851 [DEBUG] [MXServer] [CID-MXSCRIPT-358147] 
LAUNCHPOINT:SCRIPTNAME >> -------------- BEGIN SCRIPT ------------
LAUNCHPOINT:SCRIPTNAME >> Automation Script Template
LAUNCHPOINT:SCRIPTNAME >> Work Order Count: 5917
LAUNCHPOINT:SCRIPTNAME >> -------------- END SCRIPT --------------

 

Did You Know...

As Maximo Experts, we have developed several add-on products for Maximo that mobilize the work force, simplifies assignments, provides ad-hoc reporting capabilities and facilitates the seamless integration of Service Requests into Maximo.

Check out our products by clicking on the following links: EZMaxMobile, EZMaxPlanner and EZMaxRequest.

Find Out More

One thought on “Automation Script Template

  1. Nice! I have something a little different in the form of Snippets. Can’t wait to move to 7.6 to use the AS Libraries functionality. Here is my first crack at converting this to Javascript (we only use JavaScript).

    importClass(Packages.psdi.util.MXApplicationException);

    importClass(Packages.psdi.mbo.MboConstants);

    importClass(Packages.psdi.server.MXServer);

    // Examples:

    //importClass(Packages.psdi.app.workorder.WOSet);

    //importClass(Packages.psdi.app.workorder.WO);

    ////////////////////////////

    // Helper function for sending an email

    ////////////////////////////

    function sendEmail(toEmail, fromEmail, subject, body){

    MXServer.sendEMail(toEmail, fromEmail, subject, body);

    }

    ////////////////////////////

    // Helper function for printing

    ////////////////////////////

    function log(e){

    print(launchPoint + ":" + scriptName + " >> " + e + "rn");

    }

    ////////////////////////////

    // Another helper function for printing (use this if you want to use the LONGDESCRIPTION field on the MBO)

    // Uncomment this section to use it

    ////////////////////////////

    //function log(e){

    // mbo.setValue("DESCRIPTION_LONGDESCRIPTION", mbo.getString("DESCRIPTION_LONGDESCRIPTION") + " " + e);

    // Reset long description before every run

    //mbo.setValueNull("DESCRIPTION_LONGDESCRIPTION");

    //}

    ////////////////////////////

    // Helper function to set an error (see difference above from 'raiseError')

    ////////////////////////////

    //??? Not sure if this will work.

    function setError(errGroup, errKey, errParams){

    var errorkey;

    var errorgroup;

    var params;

    errorkey=errKey;

    errorgroup=errGroup;

    params=errParams;

    }

    ////////////////////////////

    // Helper function to raise an application error (see difference above from 'setError')

    ////////////////////////////

    //??? not sure if this will work.

    function raiseError(errGroup, errKey, errParams){

    raise MXApplicationException(errGroup, errKey, errParams);

    }

    ////////////////////////////

    // Helper function to get any MboSet

    ////////////////////////////

    function getMboSet(objectName){

    return MXServer.getMXServer().getMboSet(objectName, MXServer.getMXServer().getSystemUserInfo());

    }

    ////////////////////////////

    // Script code begins here

    ////////////////////////////

    print "rn";

    log("-------------- BEGIN SCRIPT ------------") ;

    log("My Automation Script Template");

    woSet = getMboSet("WORKORDER");

    log("Work Order Count: "+ woSet.count()) ;

    log("-------------- END SCRIPT --------------");

Leave a Reply