The following code snippets show how multiple custom properties can be added, updated, deleted and queried against any MES Object type.
Note that as of this time (2.9.2 SP5), using 'DateTime' as a datatype parameter does not seem to work. If you are storing a date, store it as a string and then convert it back to a datetime object when you need it.
def deleteCustomProperties(mesObject):
#This function will delete all custom properties of the passed object and return the updated object instance
cpList = mesObject.getCustomPropertiesFull()
for key in cpList:
mesObject.removeCustomProperty(key)
system.mes.saveMESObject(mesObject)
return system.mes.loadMESObject(mesObject.getUUID())
############################################################
def updateCustomProperties(mesObject, cpList):
#This function will update the passed object with the list of custom properties
#and return the updated object instance. If the custom properties do not exist, they will be created
mesObject.setCustomPropertyValues(cpList)
system.mes.saveMESObject(mesObject)
return system.mes.loadMESObject(mesObject.getUUID())
############################################################
def getCustomProperties(mesObject):
#This function returns all custom properties of the passed object
cpList = mesObject.getAllCustomProperties()
for item in cpList:
print 'Custom Property Settings for ', item.getName()
print 'Value - ', item.getValue()
print 'Units - ', item.getUnits()
print 'DataType - ', item.getIgnitionDataType()
print 'Description - ', item.getDescription()
print
############################################################
name = 'WO-0001'
objType = 'WorkOrder'
#Here we will define the custom property settings to be added to the MES Object
#Note datatypes 'DateTime' and 'Text' do not work
cpName = 'Start Date'
cpDataType = 'String'
cpVal= system.date.now()
cpDesc = 'Start Date for this WO'
cpUnits = 'No Units'
cpList = {cpName : [cpDataType , cpVal, cpDesc , cpUnits]}
#This is an example of how to pass multiple custom property settings
#cpList = {'Amps' : [current_dataType , current, current_desc , current_units], 'Volts': [volts_dataType , volts, volts_desc, volts_units]}
mesObject = system.mes.loadMESObject(name, objType)
mesObject = deleteCustomProperties(mesObject) #We only need to delete the custom property if we are trying to change the units, seems to be a bug
mesObject = updateCustomProperties(mesObject, cpList)
getCustomProperties(mesObject)