Friday, November 22, 2024

Trouble with Maximo Relationships

 

No this is not going to be advice for your love life. I ran into an issue with adding an Mbo to an MboSet that caused unexpected behavior in processing of the Record set.

First a little background. The business requirement was that for every Work Order task of type "X", an automation script needed to create a new task of type "Y" on the same work order. This automation script would be called either by an escalation or workflow. With the base mbo being the work order, I created the following code:

     woTasksMboSet = mbo.getMboSet("WOACTIVITY") 
     woTaskMbo = woTasksMboSet.moveFirst() 
     while woTaskMbo is not None: 
       if woTaskMbo.getString("TASKTYPE","X"): 
         newWoTaskMbo = woTasksMboSet().add() 
         newWoTaskMbo.setValue("TASKTYPE","Y") 
         … set fields 
       woTaskMbo = woTasksMboSet.moveNext() 

In testing, when there was only one task with type "X", the code worked fine but if there were two "X" tasks, the first record would process and then the loop would stop. After adding a few logging statements, I found that when the new record was added, it was re-organizing the records already in the woTaskMboSet object so that the record I was on became the last record. Very strange. Next I had the idea of using a setWhere to filter the records to just the records I want. That way when I add the new record, it would not meet the 'where' condition and thus not affect my current record set. So I tried the following:

     woTasksMboSet = mbo.getMboSet("WOACTIVITY") 
     woTasksMboSet.setWhere("tasktype = 'X'") 
     woTaskMbo = woTasksMboSet.moveFirst() 
     while woTaskMbo is not None: 
       newWoTaskMbo = woTasksMboSet().add() 
       newWoTaskMbo.setValue("TASKTYPE","Y") 
       … set fields 
       woTaskMbo = woTasksMboSet.moveNext() 

Sadly, this returned the same results as before, once the first record was processed, it would stop processing. Adding a '.count()’ in the logging I found that even though I was adding a new record with tasktype "Y", it was still being added to my record set, even with the setWhere() condition. My next thought was maybe the issue was that I was using the same MboSet object to loop through the records and add. So next I created a separate MboSet object just for adding records.

     woTasksMboSet = mbo.getMboSet("WOACTIVITY") 
     woTasksMboSet.setWhere("tasktype = 'X'") 
     addWOTasksMboSet = mbo.getMboSet("WOACTIVITY") 
     woTaskMbo = woTasksMboSet.moveFirst() 
     while woTaskMbo is not None: 
       newWoTaskMbo = addWOTasksMboSet().add() 
       newWoTaskMbo.setValue("TASKTYPE","Y") 
       … set fields 
       woTaskMbo = woTasksMboSet.moveNext() 

Unfortunately, this still caused the new records to be added to the woTasksMboSet object and re-arranged the order of the records. It was very frustrating. It turns out that the solution was in the relationship. As long as I use the same relationship to read and add records, the new record will affect the record set. I created a relationship that returns just records with tasktype = "X" and then used the WOACTIVITY relationship to add the record.

     woTasksMboSet = mbo.getMboSet("WOACTTASKTYPEX") 
     addWOTasksMboSet = mbo.getMboSet("WOACTIVITY") 
     woTaskMbo = woTasksMboSet.moveFirst() 
     while woTaskMbo is not None: 
       newWoTaskMbo = addWOTasksMboSet().add() 
       newWoTaskMbo.setValue("TASKTYPE","Y") 
       … set fields 
       woTaskMbo = woTasksMboSet.moveNext() 

This bit of code finally worked as it processed both Tasks with tasktype = 'X' and added two new records with tasktype = 'Y'. As long as the relationship I use for processing (WOACTTASKTYPEX) excludes the record I am adding, then it will never appear in my record set (messing up the order).

Happy Coding.

No comments:

Post a Comment