An MboSet is a set of MBOs, so in order to work with each Mbo of this set it is necessary to make a loop to be able to navigate within this MboSet.
This is how we could loop through the MboSet:
Using MboSet.count()
from psdi.mbo import Mbo
from psdi.mbo import MboSet
poMbo = mbo
# Instantiate a MboSet of POLINE
polineMboSet = poMbo.getMboSet("POLINE")
# Validate if the MboSet is not null or empty
if polineMboSet is not None and not polineMboSet.notExist():
# Using for loop
for i in range (0, polineMboSet.count()):
polineMbo = polineMboSet.getMbo(i)
print "Item number: ", polineMbo.getString("ITEMNUM")
# Using while loop
i = 0
while i < polineMboSet.count():
polineMbo = polineMboSet.getMbo(i)
print "Item number: ", polineMbo.getString("ITEMNUM")
Instantiate psdi.mbo.MboSetEnumeration class
from psdi.mbo import Mbo
from psdi.mbo import MboSet
# Import MboSetEnumeration class
from psdi.mbo import MboSetEnumeration
poMbo = mbo
# Instantiate a MboSet of POLINE
polineMboSet = poMbo.getMboSet("POLINE")
# Validate if the MboSet is not null or empty
if polineMboSet is not None and not polineMboSet.notExist():
# Instantiate MboSetEnumeration based on the MboSet
polineMboSetEnum = MboSetEnumeration(polineMboSet)
# Loop through the MboSetEnumeration until it has no more elements
while polineMboSetEnum.hasMoreElements():
# Instantiate the next Mbo of the MboSet
polineMbo = polineMboSetEnum.nextMbo()
print "Item number: ", polineMbo.getString("ITEMNUM")
Using MboSet.moveNext()
from psdi.mbo import Mbo
from psdi.mbo import MboSet
poMbo = mbo # @UndefinedVariable
# Instantiate a MboSet of POLINE
polineMboSet = poMbo.getMboSet("POLINE")
# Validate if the MboSet is not null or empty
if polineMboSet is not None and not polineMboSet.notExist():
# Instantiate the first Mbo of the MboSet
polineMbo = polineMboSet.moveFirst()
# Loop until the Mbo is null
while polineMbo:
print "Item number: ", polineMbo.getString("ITEMNUM")
# Instantiate the next Mbo of the MboSet
polineMbo = polineMboSet.moveNext()
Recommendations
I do not recommend the first option because it is not optimal, since for each iteration of the loop a SELECT COUNT(*) query is performed on the database. This creates unnecessary overhead for the database server. Also, we need a variable for the index (i).
The second option is good from a performance point of view, we avoid overloading the database. However, we need to import an additional library psdi.mbo.MboSetEnumeration and instantiate it. Also, it involves 2 additional lines of code compared to the third option.
The third option is personally the best. From a code point of view, it’s cleaner and shorter.
If you found my post interesting or useful and just want to say thanks, you can always buy me a coffee.



