Search code examples
pythonmaximo

Unable to fetch the second mbo for poline using automation script in maximo


I am trying to fetch the second line in POLINE however the below script continues to fetch the first line itself again instead of second one. On investigating the count of the polineset is 2 however the same line gets fetched again. I am not sure what could be the root cause, the script is invoked on PO object launch point

if mbo.getMboValue("STATUS").isModified() and mbo.getString("STATUS") =='APPR' :
po = mbo.getMboSet("POLINE")
polineSet = po.count()
    print "count of poline", polineSet >> returns 2 rows
if (polineSet > 0): 
    for i in range(0,polineSet):
        poline = po.getMbo(i)
        description = poline.getString("description")
        print" po line number" poline.getInt("polinenum") >> prints 1 twice 
        if (((poline.getString("LINETYPE") =='ITEM' or poline.getString("LINETYPE") == 'MATERIAL'))):
            newpoline= po.add(2L)
            newpoline.setValue("linetype",'XXX')
            newpoline.setValue("description",description + '- XXX' + str(linenum))

Updated code with moveFirst()

if mbo.getMboValue("STATUS").isModified() and mbo.getString("STATUS") =='APPR' :
poLineSet = mbo.getMboSet("POLINE")
poLine = poLineSet.moveFirst()
while (poLine):
    description = poLine.getString("description")
    linenum = poLine.getString("polinenum")
    if (((poLine.getString("LINETYPE") =='ITEM' or poLine.getString("LINETYPE") == 'MATERIAL'))):
        newpoline= poLineSet.addAtEnd(2L)
        newpoline.setValue("linetype",'XXX')
        newpoline.setValue("description",description + '- XXX'+ str(linenum))
        poLine = poLineSet.moveNext()

Solution

  • I assume your "po.add(2L)" line is running in this test? The "add" method adds the new MBO to the beginning of the MBO set, thereby pushing your original first MBO into the second slot, and the original second MBO into the third slot. That means that when your loop starts over and gets MBO 2, you get what's newly in the second slot, meaning the first MBO. Your loop ends before it reaches the new-third slot (where the next MBO you want has moved to) because you constructed your condition to only do up to the original count. In fact, since it processed that first MBO again, it would have added a second MBO to the set, pushing your originally-first MBO into the third slot. So if your loop was set up to keep going, you would just keep adding new MBOs and keep pushing that same first MBO on to the next processing slot indefinitely.

    What you probably want in this case is "addAtEnd()" (and to keep your loop as-is). Alternatively you can use the MBO set iterating methods of .moveFirst() and .moveNext(). I think that will keep you advancing through the set even if you add records behind or at the current position.