MboValueAdapter – Initial, Previous and Current Value of a Field

The Theory

Sometimes when you work with a Mbo in a MboSet and you change fields in a Mbo it can be useful to get the original value of the field before the change or the last value of the field before the change. So basically a field can have three different values we can ask for:

valudata2

For that purpose we can use the MboValueAdapter and MboValue classes which are automatically initialized with every Mbo retrieved from the system

The CurrentValue of  a field usually can be red by the following Method via a Mbo:

wonum = mbo.getString("WONUM") # wonum is of type string

The same action can also be achived using the MboValueAdapter:

mboValue = mbo.getMboValue("WONUM") # mboValue is of Type mboValue 
currentValue = mboValue.getCurrentValue() # currentValue is of type MaxType
wonum = currentValue.asString() # wonum is of type string

# The previous 3 lines can be merged to a shorter Version:
wonum = mbo.getMboValue("WONUM").getCurrentValue().asString()

Now the short version to get the PreviousValue which can only be get via the MboValueAdapter:

wonum = mbo.getMboValue("WONUM").getPreviousValue().asString()

And the InitialValue which represents the state of the field when the MboSet was initialized:

# Version via Mbo:
wonum = mbo.getDatabaseValue("WONUM")

# Version via ValueAdapter
wonum = mbo.getMboValue("WONUM").getInitialValue().asString()

 

A practical Example

The scenario I will show you in this blog is based on the Item Mbo and the description field. I will change this field several times and always show you the initial, previous and current value of this field. Precisely we will do the following:

valuedata

As you can see we have 3 point where we verify our output. The following script can be easily created as a new Automation Script without launchpoint. Just take it and try it out! You can run the Script directly from the Automation Script Application.

#AUTOSCRIPT:MBOVALUEADAPTER
#DESCRIPTION:Demo to show usage of mboValueAdapter
#LOGLEVEL:ERROR
from psdi.server import MXServer
from psdi.iface.mic import MicService

# Initialize some stuff
mxServer = MXServer.getMXServer()
micService = MicService(mxServer)
micService.init()
userInfo = micService.getNewUserInfo()

itemSet = mxServer.getMboSet('ITEM', userInfo)
itemMbo = itemSet.moveFirst()
 
# Get Value for Field Description
stringValue = itemMbo.getString("DESCRIPTION")
itemMbo.setValue("DESCRIPTION", stringValue + "_new")

# Get the new Value again
stringValueNew = itemMbo.getString("DESCRIPTION")
itemMbo.setValue("DESCRIPTION", stringValue + "_new_new")

# Get the new Value again
stringValueNewNew = itemMbo.getString("DESCRIPTION")

print "Original Value              = " + stringValue
print "Value after 1. Change       = " + stringValueNew
print "Value after 2. Change       = " + stringValueNewNew


# Now work with the new MboValue Class
mboValue = itemMbo.getMboValue("DESCRIPTION")
initialValueAsString = mboValue.getInitialValue().asString()
previousValueAsString = mboValue.getPreviousValue().asString()
currentValueAsString = mboValue.getCurrentValue().asString()

print "-----------------------------------------------------------------"
print "Now Mbo Value Results"
print "-----------------------------------------------------------------"
print "Initial Value               = " + initialValueAsString
print "Previous Value before 2. change = " + previousValueAsString
print "Current Value from MboValue = " + currentValueAsString

itemSet.save()

print "-----------------------------------------------------------------"
print "Now After itemSet.save()"
print "-----------------------------------------------------------------"

mboValue = itemMbo.getMboValue("DESCRIPTION")
initialValueAsString = mboValue.getInitialValue().asString()
previousValueAsString = mboValue.getPreviousValue().asString()
currentValueAsString = mboValue.getCurrentValue().asString()

print "Initial Value               = " + initialValueAsString
print "Previous Value before 2. change = " + previousValueAsString
print "Current Value from MboValue = " + currentValueAsString

itemSet.reset()
itemSet = mxServer.getMboSet('ITEM', userInfo)
itemMbo = itemSet.moveFirst()

print "-----------------------------------------------------------------"
print "Now After itemSet.reset()"
print "-----------------------------------------------------------------"

mboValue = itemMbo.getMboValue("DESCRIPTION")
initialValueAsString = mboValue.getInitialValue().asString()
previousValueAsString = mboValue.getPreviousValue().asString()
currentValueAsString = mboValue.getCurrentValue().asString()

print "Initial Value               = " + initialValueAsString
print "Previous Value before 2. change = " + previousValueAsString
print "Current Value from MboValue = " + currentValueAsString

The output should look similar to the following:

Original Value              = IT Services
Value after 1. Change       = IT Services_new
Value after 2. Change       = IT Services_new_new
-----------------------------------------------------------------
Now Mbo Value Results
-----------------------------------------------------------------
Initial Value               = IT Services
Previous Value before 2. change = IT Services_new
Current Value from MboValue = IT Services_new_new
-----------------------------------------------------------------
Now After itemSet.save()
-----------------------------------------------------------------
Initial Value               = IT Services
Previous Value before 2. change = IT Services_new
Current Value from MboValue = IT Services_new_new
-----------------------------------------------------------------
Now After itemSet.reset()
-----------------------------------------------------------------
Initial Value               = IT Services_new_new
Previous Value before 2. change = IT Services_new_new
Current Value from MboValue = IT Services_new_new