Wednesday, July 6, 2011

Stop and Restart Workflow through Action Class

There are several scenarios where a Maximo application object (workorder / jobplan / pm) which is currently in a workflow needs to be stopped and restarted. I faced one such instance recently where the workflow design was modified to fix a flaw and there were several workorders which were tagged to the old workflow version.  Hence I had to stop workorders in the old workflow and restart them in the new workflow. As the count was huge, it was impossible to do it manually using the Workflow administrator to stop the workflow and restart them using the Workorder application. Hence I wrote an action class and tagged it to an escalation which will have a condition to pull all the workorders that needs to be stopped and restarted.

I am explaining this example with WORKORDER as the object which is in the old workflow. The workorder needs to be stopped from the old workflow and re-routed to the newly revised workflow

Here is what you need to do....
1. Create a Relationship to find the active WFINSTANCE records for the workorder.
   
Name: ACTIVEWFINSTANCE
Child Object: WFINSTANCE
Where Clause: ownerid=:workorderid and ownertable=’WORKORDER’ and active=1
Remarks: Gets all the Active Workorders from workorder table which has pending assignments

2. Create a new Action Class and associate it to a Custom Action.
3. Here is the action class.

package custom.escalation.action;

import java.rmi.RemoteException;
import psdi.mbo.MboRemote;
import psdi.mbo.SqlFormat;
import psdi.util.MXException;
import psdi.workflow.WFInstanceSetRemote;
import psdi.workflow.WFInstance;
import psdi.util.logging.MXLogger;
import psdi.util.logging.MXLoggerFactory;
import psdi.workflow.WFProcess;
import psdi.workflow.WFProcessSetRemote;




public class StopWorkFlow implements
psdi.common.action.ActionCustomClass
{

     private static final MXLogger log = MXLoggerFactory.getLogger("maximo");
   
    public void applyCustomAction(MboRemote mbo, Object[] arg)
    throws MXException, RemoteException
    {
    log.debug("Entered applyCustomAction of StopWorkFlow");
    WFInstanceSetRemote wfInstanceSet=(WFInstanceSetRemote) mbo.getMboSet("ACTIVEWFINSTANCE");

   
        if(!wfInstanceSet.isEmpty())
        {
            log.debug("WfInstance is not empty");
            for(int wfInstance=0; wfInstance <wfInstanceSet.count();wfInstance++)
            {
            WFInstance wfInst=(WFInstance) wfInstanceSet.getMbo(wfInstance);
            String processName = wfInst.getString("processname");
            log.debug("processName: "+ processName);           
            WFProcessSetRemote wfProcessSet = (WFProcessSetRemote) mbo.getMboSet("WFPROCESS");
            SqlFormat sqf1 = new SqlFormat(mbo.getUserInfo(), "processname = :1 and active = 1");
             sqf1.setObject(1,"WFPROCESS","PROCESSNAME", processName);
             wfProcessSet.setWhere(sqf1.format());
            int matchingProcess = wfProcessSet.count();
            log.debug("matchingProcess: " + matchingProcess);
            WFProcess wfProcess = null;
            
            if (matchingProcess==1)
            {
                wfProcess = (WFProcess)wfProcessSet.getMbo(0);
            }
           
            // Enter memo for the closed transaction
            wfInst.stopWorkflow("Workflow stopped through escalation");
            log.debug("After stopping workflow");
            // Enter memo for restarted transaction
            wfInst.initiateWorkflow("Initiated workflow through escalation",wfProcess);
            log.debug("After initiating workflow");
            wfInstanceSet.save();
           


            } //end of For
        } //End of if(!wfInstanceSet.isEmpty())
    } //End of public void applyCustomAction
} //End of stopWorkFlow


 4. The action class gets the process name that the workflow is currently in and retrieves the active version of the process. It then stops the workflow of the existing workorder and then restarts it using the latest active version of the workflow.

5. Now create an escalation and associate a condition to pick the relevant workorders which needs to be stopped and restarted.

6. Associate an action to the escalation which has to be tagged to the action class created in Step 3.

No comments:

Post a Comment