MBOs in Automation Scripts: Update Attributes of an Mbo
In our previous posts in this series, we talked about how an MboSet is a collection of Mbo objects. This is analogous to a spreadsheet of data representing an MboSet, and a single row within that spreadsheet representing an Mbo. This article will discuss how to update single attributes on an Mbo, or in our spreadsheet analogy, update data within a single cell within a row. To modify an object in the collection, the object must first be found. Most of the time, the launch points in automation scripts will supply an implicit variable called mbo that refers to the current record. Alternatively, you can get a handle to an Mbo from an MboSet as described in previous articles of this series – remember the
moveFirst()methods. Once you have an Mbo, changes are made using the
setValue()method. As is the case with other methods,
setValue()is a member of both the Mbo and MboSet classes. Our first example changes the description of the current Mbo (a work order) by calling the mbo variable’s version of the setValue() method.
mbo.setValue("DESCRIPTION", "Changed the description")
setValue()method, passing the second argument as a string works for all attributes, regardless of their underlying data types. For example, if an attribute is a numeric type, passing the string “1” is allowed. There are also setValue() method calls that take the long, int, Date, boolean, float, or double data types as the second argument. This call will throw an exception if the underlying data type does not match the type of the value passed in the call. For example the following code does not work.
A call to set the value of a particular attribute may result in the values of other attributes changing. For example, setting the “assetnum” attribute of a work order may result in the “location” attribute being updated as well. To check the new value, getString() could be used on the “location” attribute. Once all values have been modified, the save() method can then be called.
This saves and commits all changes to the database. In most cases, an explicit call to
save()is not necessary. In cases where the automation script fires before the save event, the save event will trigger automatically after the execution of the script. If there is an error during the save, an exception is thrown and the changes are not saved. In that case, the collection is left in exactly the same state as prior to the save() call. This allows appropriate action to be taken and the save() call to be resubmitted. There is a third argument that can be passed to the
setValue()method, which is the Access Modifier. This should only be used when you are certain of the implications of using it. In essence, standard application logic can be bypassed by using access modifiers, which is why they should be used only when necessary. Standard access modifiers are:
- NOACCESSCHECK: Suppresses the “Field is Read Only” message and performs the update regardless of whether the field is editable or not.
- NOVALIDATION: Update the field without validating the data being entered.
- NOACTION: Do not perform related system actions when updating. For example, do not update the location on a work order when updating the asset.
To update a field and not perform the validation and action events:
mbo.setValue("DESCRIPTION", "Update a read only field", mbo.NOACCESSCHECK)
Putting all three together:
mbo.setValue("DESCRIPTION", "Update a field without validation or action", mbo.NOVALIDATION|mbo.NOACTION)
A list of attributes whose values may be modified and the objects to which they belong can be found in the MAXATTRIBUTE table in the MAXIMO database. The OBJECTNAME column of this table corresponds to the object name. The ATTRIBUTENAME column corresponds to the attribute name. Our next article will focus on how to add new Mbo records into an MboSet.
mbo.setValue("DESCRIPTION", "Please be careful doing this", mbo.NOACCESSCHECK|mbo.NOVALIDATION|mbo.NOACTION)
Preventing fields from Editing Once Record is in Workflow I like this problem because it involves a few...
How to enable the print button in Maximo. By default, Maximo only allows the MAXADMIN group to have...