When you are adding or updating records in Maximo via code (e.g. Java, Autoscript, etc), then you need to understand Maximo transactions and when you are adding something in the current transaction (so when the user clicks 'save', you record will be saved) or when you have created a new transaction (and you have to save it via code).
I recently had this requirement: when a user tries to save an asset with a location, the system must go out and look at the location for other assets. If there are no other assets attached to the location, then add a dummy asset and attach it to the asset also. (Yes there was a functional reason for this.)
This is a really good example to show both ways of working with transactions in Maximo. For the new asset, we can either attached it to the current save so when the save process finishes, it will also save our new asset. This has a benefit of, should something go wrong on the save, then our changes are also not saved. Here is the code snippet (autoscript):
...
# get Locatoins MBO Set. this is the key our transaction
locationMboSet = mbo.getMboSet("LOCATIONS")
# get current location record
curLocation = locationMboSet.getMbo(0)
# get all assets for the current location
assetMboSet = curLocation.getMboSet("ASSETS")
# don't look at the current record (in case the users is updating the record).
assetMboSet.setWhere("assetnum != " + mbo.getString("ASSETNUM") + "'")
assetMboset.reset()
# check for no records
if assetMboSet.count() == 0:
# add dummy asset
dummyAsset = assetMboSet.add()
dummyAsset.setValue("assetnum", "DUMMY"+ mbo.getString("assetuid").substring(7))
dummyAsset.setValue("description","DUMMY place holder Asset do not use")
...
In our example how we got the Locations MBO Set is very import. You will notice that I used the current mbo (the getMboSet() method). By doing this, it keeps that record set as part of the current transaction in Maximo. So later in the code, when I add the new Asset, it is also part of the same transaction and Maximo will save or roll it back with the main record that is being added/updated. Doing it this way you would never want to perform a save via code as it would cause an error to the user when the save tried to happen within Maximo (the dreaded "record has been updated by another user").
However, had I used this line of code to get the locations MBO Set:
...
locationMboSet = MXServer.getMXServer().getMboSet("LOCATIONS",...)
...
Maximo would have created a new transaction for the locations MBO set. That means that for this code to work, I would have to add a locationMboSet.save(). Without the save(), I would lose my new asset record. The other thing is that since I have done my save, the new asset would exist before the user has officially saved theirs. (This transaction completes before the Maximo transaction does.)
Both methods have their place and I have used both. It is important to understand how to use each and what that means to your code.
Happy Coding
No comments:
Post a Comment