Handling workflow from automation script

If you need to handle workflows (start, stop, route, etc.) from an automation script you can use the following two Maximo API classes: WorkFlowService and WFInstance. Here are few examples.

The WorkFlowService.initiateWorkflow metod can be used to start a workflow:

from psdi.server import MXServer
# get the workflow service
wfSrv = MXServer.getMXServer().lookup('WORKFLOW')
# start the workflow
wfSrv.initiateWorkflow("MYWF", mbo)

To stop an active workflow you can use WFInstance.stopWorkflow method:

from psdi.server import MXServer
# get the workflow service
wfSrv = MXServer.getMXServer().lookup('WORKFLOW')
# get active instances for the current mbo
wfSet = wfSrv.getActiveInstances(mbo)
# stop the first workflow instance
wfSet.getMbo(0).stopWorkflow("Stopped")

To route a workflow I have used the WorkFlowService.completeAssignment method. Here it is a little more complex since we first have to get the right WFAssignment record to the current Mbo and user.

from psdi.mbo import SqlFormat
from psdi.server import MXServer

def getMboAssignments(mbo, personid):
  sqf = SqlFormat(mbo, "ownertable=:1 and wfassignment.ownerid=:2 and assigncode=:3 and assignstatus='ACTIVE'")
  sqf.setObject(1, "WFASSIGNMENT", "OWNERTABLE", mbo.getName())
  sqf.setLong(2, mbo.getUniqueIDValue())
  sqf.setObject(3, "WFASSIGNMENT", "ASSIGNCODE", personid)
  assignSet = mbo.getMboSet("$CURMBOASGN", "WFASSIGNMENT", sqf.format())
  assignSet.reset()
  return assignSet

wfSrv = MXServer.getMXServer().lookup('WORKFLOW')
wfAsSet = getMboAssignments(mbo, mbo.getUserInfo().getPersonId())
if not wfAsSet.isEmpty():
  wfAs = wfAsSet.getMbo(0)
  wfSrv.completeAssignment(wfAs, "Approved by script", True)
  

Handling workflow from automation script

5 thoughts on “Handling workflow from automation script

  1. Hello there, Bruno.
    I tried to use your “stop an active workflow” example, but did not work. I got error “AttributeError: ‘NoneType’ object has no attribute ‘stopWorkflow'”.
    After researching I tried something and got the following, this worked:

    def fecharWorkflowAtivo(curMbo):
    wfAssignmentSet = curMbo.getMboSet(“$wfassignment”, “WFASSIGNMENT”, “ownertable=’WORKORDER’ and ownerid=:workorderid and assignstatus=’ACTIVE'”)
    wfAssignment = wfAssignmentSet.getMbo(0)
    if wfAssignment is not None:
    wfAssignment.close()

    1. @Rodrigo
      I am thinking if this will still work if the Workflow is in wait node.

  2. I also tried the stop Workflow on object launch point before and after save as well, but it is not working.
    Any other way of doing that? As i want to stop a workflow when the user changes the status to canceled of a SR from “Change Status” at any stage of Workflow.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top