To post error messages, you set the predefined errorgroup implicit variable and the predefined errorkey implicit variable to the error message group and error key. You can also post error messages by using the MXException class in the API.
You use can use the public MXException class and its constructor method. You pass in the message group ID, message key ID, and any parameters for the message into the constructor method MXException. You use the Jython keyword raise before the MXException constructor method to send the error message to the user interface.
In the following script sample, the error to indicate that an invalid prefix on a new asset is used as an example:
from psdi.util.import MXApplicationException
⋮
if<some condition>:
params = [prefix, assettype]
raise MXException('asset','invalidassetprefix', params)
You can raise an error message from the script code by using the implicit variables that are available in the scripting framework. You set the errorgroup implicit variable to the error message group. You set the errorkey implicit variable to the error key. You set the params implicit variable to the prefix and object type, such as an asset.
When execution is complete, the scripting framework detects the error flag and the framework sends the corresponding exception for the error group and error key combination. After the error flags are set in the script, the script execution continues.
When a new asset record is created and a user clicks Save, the script tests the asset number for a valid prefix. If the prefix is not found, the user receives an error message. The predefined message is picked up by the scripting framework and placed into the predefined variables.
Prefix and assettype are script variables in the following sample Jython code:
def setError(prefix):
global errorkey, errorgroup, params
errorkey=invalidassetprefix
errorgroup=asset
params=[prefix, assettype]
if atype_internal=='FACILITIES' and not anum.startswith('FT'):
setError('FT')
elif atype_internal=='FLEET' and not anum.startswith('FL'):
setError('FL')
elif atype_internal=='IT' and not anum.startswith ('IT'):
setError('IT')
elif atype_internal=='PRODUCTION' and not anum.startswith ('PR'):
setError('PR')
When the script code test of the asset number prefix fails, the code invokes a common function within the script. The script passes parameters for the function to use in the message. The common function sets the errorgroup, errorkey, and params implicit variables to the appropriate message that is defined in the Database Configuration application. The scripting framework recognizes that the implicit variables are used in the script, and after the script execution is finished, the scripting framework pushes the error message to the application user interface.