Loop MboSet and output Mbo content

A very common programming pattern is to loop on a MboSet and read information from the included Mbo’s. This example can be technical combined with the RMI introduction to get a quick result since it requires a valid session object.

In this example I would like to show how to loop on all existing workorders in the system. Later on we will see how to select specific records from a MboSet, but for now let’s start with all workorders in the system. The following pattern can be used to achieve that objective and print out the workorder numbers:

woset = session.getMboSet('WORKORDER')

for i in range(0,woset.count()):

    # get one Workorder Mbo
    wo = woset.getMbo(i)

    # Read out a value
    print "Workorder ",wo.getString("WONUM")

Some of the important commands in this example are:

  • woset = session.getMboSet(‘WORKORDER’) – method to get a MboSet and stores this in the variable woset – in that case based on the session object
  • woset.count() – returns the number of records in woset
  • woset.getMbo(i) – gets the Mbo with index i
  • wo.getString(“WONUM”) – returns the string from DB Field WONUM from the Workorder Object

As always in programming there are often different ways to achieve the same goal and so I would like to introduce a different way on how it is possible to loop on a MboSet:

woset = session.getMboSet('WORKORDER')
wo = woset.moveFirst()

while (wo):
    # Read out a value
    print "Workorder ",wo.getString("WONUM")

    # get one Workorder
    wo = woset.moveNext()

In that case we use the methods of the MboSet object to navigate around using some simple commands:

  • wo = woset.moveFirst() – gets the first record from the woset
  • woset.moveNext() – moves the pointer to the next record in woset
  • while (wo != None) – loops as long as wo has a valid record (not at the end)

Using RMI from a Jython Script

The Remote Method Invocation (RMI) Interface of TPAE allows to connect from an external program to TPAE. Using RMI business objects can be red, written and updated by the control of a Jython script. To use RMI Scripting a current version of Jython has to be installed on the system where the script should be executed.

The script then connects to the RMI Port of TPAE which is by default Port 1099 (Maximo up to Version 6) or 13400 (Maximo 7 and higher). This port can be changed using the mxe.registry.port property. The following picture shows the concept:

RMI Base concept

Based on this knowledge we can develope our first RMI Script, which only connects to the MX Server and after that disconnects:

import psdi.util.MXSession as MXSession

session = MXSession.getSession()
session.setHost('192.168.158.135:13400/MXServer')
session.setUserName('maxadmin')
session.setPassword('maxadmin')
session.connect()

# ... Do your stuff here ...

session.disconnect()

One word to the Path “MXServer” in the setHost() function call. This has to be the name, which is provided in the Maximo Properties in the parameter mxe.name. This means, that you can have several RMI listeners for multiple Maximo installations on the same host (e.g. mx.name = MXTest, mx.name = MXProd).

In a second step the script can be extended to correctly handle error situations. To achive this you can utilize the Jython error handling functionality:

try:
# perform some task that may raise an exception

except Exception, value:
# perform some exception handling

finally:
# perform tasks that must always be completed (Will be performed before the exception is # raised.)

The new connection section of the script now looks as follows:

import psdi.util.MXSession as MXSession
from psdi.util import MXException

try:
    session = MXSession.getSession()
    session.setHost('192.168.158.135:13400/MXServer')
    session.setUserName('maxadmin')
    session.setPassword('maxadmin')
    session.connect()

except MXException, conex:
    print 'conex.getErrorGroup()     :',conex.getErrorGroup()
    print 'conex.getErrorKey()       :',conex.getErrorKey()
    print 'conex.getDetail()         :',conex.getDetail()
    print 'conex.getDisplayMessage() :',conex.getDisplayMessage()
    exit

# ... Do your stuff here ...
1 2 3